Find the K-th Character in String Game II - Problem
Find the K-th Character in String Game II

Alice and Bob are playing an exciting string transformation game! ๐ŸŽฎ

Alice starts with a simple string word = "a". Bob gives her a sequence of operations to perform, and after all operations are complete, he wants to know what the k-th character in the final string will be.

The Rules:
โ€ข Operation 0: Append a copy of the current word to itself (e.g., "abc" โ†’ "abcabc")
โ€ข Operation 1: Generate a new string by shifting each character to its next letter in the alphabet, then append it to the original word (e.g., "abc" โ†’ "abcbcd")

Note: The letter 'z' wraps around to 'a' in operation 1.

Goal: Return the k-th character (1-indexed) in the final string after performing all operations in sequence.

Example walkthrough:
Starting with "a" and operations [1, 0]:
1. Operation 1: "a" becomes "ab" (append shifted version)
2. Operation 0: "ab" becomes "abab" (append copy)
Final string: "abab"

Input & Output

example_1.py โ€” Basic Operations
$ Input: k = 5, operations = [1, 0, 1]
โ€บ Output: 'c'
๐Ÿ’ก Note: Starting with 'a': Operation 1 โ†’ 'ab', Operation 0 โ†’ 'abab', Operation 1 โ†’ 'ababcaca'. The 5th character is 'c'.
example_2.py โ€” Simple Case
$ Input: k = 2, operations = [1]
โ€บ Output: 'b'
๐Ÿ’ก Note: Starting with 'a': Operation 1 creates shifted version 'b' and appends it โ†’ 'ab'. The 2nd character is 'b'.
example_3.py โ€” Copy Operations
$ Input: k = 3, operations = [0, 0]
โ€บ Output: 'a'
๐Ÿ’ก Note: Starting with 'a': Operation 0 โ†’ 'aa', Operation 0 โ†’ 'aaaa'. The 3rd character is 'a'.

Constraints

  • 1 โ‰ค k โ‰ค 1014
  • 1 โ‰ค operations.length โ‰ค 100
  • operations[i] is either 0 or 1
  • It is guaranteed that k is valid for the final string length

Visualization

Tap to expand
String Game Binary Tree Traversal"a""a"op=1"b"+shift"a""a""b""b""c"k=5Recursive Trace-Back Process1. solve(k=5, op_idx=2): k > mid(4), so k is in second half2. solve(k=1, op_idx=1): Recurse with adjusted position k=5-4=13. solve(k=1, op_idx=0): k โ‰ค mid(1), return solve(k=1, op_idx=-1) = 'a'4. Apply shifts: 'a' + 2 shifts = 'c' (Final Answer)
Understanding the Visualization
1
Identify Position
Start with target position k and current operation index
2
Binary Split Decision
Determine if k falls in the original half or the appended half of current string
3
Trace Backwards
If in second half, adjust position and potentially count a shift
4
Apply Accumulated Shifts
Once we reach base case 'a', apply all counted shifts to get final character
Key Takeaway
๐ŸŽฏ Key Insight: Instead of building exponentially growing strings, we use the binary tree structure of operations to mathematically navigate to any position in O(n) time!
Asked in
Google 45 Meta 38 Microsoft 32 Amazon 28
43.6K Views
High Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen