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

Alice and Bob are playing a string transformation game. Initially, Alice has a string word = "a". You are given a positive integer k.

Bob will ask Alice to perform the following operation repeatedly:

  • Generate a new string by changing each character in word to its next character in the English alphabet
  • Append this new string to the original word

For example:

  • Performing the operation on "c" generates "cd" (c → d, then append)
  • Performing the operation on "zb" generates "zbac" (z → a, b → c, then append)

Return the value of the k-th character in word, after enough operations have been performed for word to have at least k characters.

Input & Output

Example 1 — Basic Case
$ Input: k = 5
Output: b
💡 Note: Step 0: "a" → Step 1: "ab" → Step 2: "abbc" → The 5th character (index 4) is 'b'
Example 2 — Small k
$ Input: k = 1
Output: a
💡 Note: The 1st character is always 'a' since we start with "a"
Example 3 — Edge Case
$ Input: k = 4
Output: c
💡 Note: Step 0: "a" → Step 1: "ab" → Step 2: "abbc" → The 4th character is 'c'

Constraints

  • 1 ≤ k ≤ 104

Visualization

Tap to expand
String Game: Find K-th Characterk = 5InputString Evolution:Step 0: "a"Step 1: "ab" (a→b, append)Output: b5th characterPattern: String length doubles each stepBinary representation of (k-1) reveals transformationsEfficient Solution: O(log k) using bit manipulationk-1=4=100₂ → 1 bit set → b
Understanding the Visualization
1
Input
k = 5 (find 5th character)
2
Process
String grows: a → ab → abbc → ...
3
Output
5th character is 'b'
Key Takeaway
🎯 Key Insight: The binary representation of (k-1) directly tells us how many character transformations to apply
Asked in
Meta 15 Google 12
23.0K Views
Medium Frequency
~15 min Avg. Time
850 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