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
The Rules:
โข Operation 0: Append a copy of the current word to itself (e.g.,
โข 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.,
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
1. Operation 1:
2. Operation 0:
Final string:
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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code