Skip to content

Commit ef7a9c4

Browse files
authored
Create Find the K-th Character in String Game II.py
1 parent 7e50700 commit ef7a9c4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Alice and Bob are playing a game. Initially, Alice has a string word = "a".
3+
4+
You are given a positive integer k. You are also given an integer array operations, where operations[i] represents the type of the ith operation.
5+
6+
Now Bob will ask Alice to perform all operations in sequence:
7+
8+
If operations[i] == 0, append a copy of word to itself.
9+
If operations[i] == 1, generate a new string by changing each character in word to its next character in the English alphabet, and append it to the original word. For example, performing the operation on "c" generates "cd" and performing the operation on "zb" generates "zbac".
10+
Return the value of the kth character in word after performing all the operations.
11+
12+
Note that the character 'z' can be changed to 'a' in the second type of operation.
13+
'''
14+
15+
class Solution:
16+
def kthCharacter(self, k: int, operations: List[int]) -> str:
17+
n, i = 1, 0
18+
while n < k:
19+
n *= 2
20+
i += 1
21+
d = 0
22+
while n > 1:
23+
if k > n // 2:
24+
k -= n // 2
25+
d += operations[i - 1]
26+
n //= 2
27+
i -= 1
28+
return chr(d % 26 + ord("a"))

0 commit comments

Comments
 (0)