Find the K-th Character in String Game I

Alice starts with word = "a". In each operation she builds a new string by changing every character of word to the next letter of the alphabet (z becomes a) and appends it to the end of word. Given a positive integer k, return the k-th character (1-indexed) of word once it has at least k characters.

30:00

Find the K-th Character in String Game I
easy
Free written solution
Topics
Companies

Alice starts with word = "a". In each operation she builds a new string by changing every character of word to the next letter of the alphabet (z becomes a) and appends it to the end of word. Given a positive integer k, return the k-th character (1-indexed) of word once it has at least k characters.

Example 1:
Input: {"k":5}
Output: b
Constraints:
  • 1≤k≤5001 \leq k \leq 500

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"k":5}

Each round doubles the word: the new right half is the old word with every letter moved up by one. So word[i + 2^m] = word[i] + 1 for i < 2^m.

The word after each doubling

pos = 4

1
a
2
a
b
4
a
b
b
c
8
a
b
b
c
b
c
c
d

k - 1 = 4: one jump per 1 bit

shifts = 0 (a)

1
4
0
2
0
1
Variables
VariableValue
k5
DepthFunction Call
Stack empty
0/7