3304. Find the K-th Character in String Game I
Alice and Bob are playing a game. Initially, Alice has a string
word = "a".You are given a positive integer
k.Now Bob will ask Alice to perform the following operation forever:
- Generate a new string by changing each character in
wordto its next character in the English alphabet, and append it to the originalword.For example, performing the operation on
"c"generates"cd"and performing the operation on"zb"generates"zbac".Return the value of the
kthcharacter inword, after enough operations have been done forwordto have at leastkcharacters.Note that the character
'z'can be changed to'a'in the operation.Example 1:
Input: k = 5
Output: "b"
Explanation:
Initially,
word = "a". We need to do the operation three times:
- Generated string is
"b",wordbecomes"ab".- Generated string is
"bc",wordbecomes"abbc".- Generated string is
"bccd",wordbecomes"abbcbccd".Example 2:
Input: k = 10
Output: "c"
def kthCharacter(self, k: int) -> str:
count = 0
times = math.ceil(math.log2(k))
s = 'a'
for i in range(times):
newstr = ''
for j in s:
newstr += chr(ord(j) + 1)
s = s + newstr
return s[k-1]