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

Alice and Bob are playing an alphabet transformation game! Alice starts with a simple string word = "a", but Bob challenges her to keep expanding it using a special rule.

The Rule: In each operation, Alice must:

  1. Take each character in the current string
  2. Transform it to the next character in the alphabet (a→b, b→c, ..., z→a)
  3. Append this transformed string to the original

Example transformations:

  • "a" becomes "ab" (a + b)
  • "ab" becomes "abbc" (ab + bc)
  • "abbc" becomes "abbcbccd" (abbc + bccd)

Your task: Given a positive integer k, find the k-th character (1-indexed) in the final string after performing enough operations to have at least k characters.

Input & Output

example_1.py — Basic case
$ Input: k = 5
Output: b
💡 Note: String evolution: 'a' → 'ab' → 'abbc' → 'abbcbccd'. The 5th character (1-indexed) is 'b'.
example_2.py — First character
$ Input: k = 1
Output: a
💡 Note: The first character is always 'a' since we start with word = 'a'.
example_3.py — Power of 2
$ Input: k = 4
Output: c
💡 Note: After 2 operations: 'abbcbccd'. The 4th character is 'c', which underwent 2 transformations (a→b→c).

Constraints

  • 1 ≤ k ≤ 105
  • The answer will always be a lowercase English letter
  • k is 1-indexed (first character is at position 1)

Visualization

Tap to expand
Mathematical Pattern RecognitionGeneration 0:alength: 1Generation 1:ablength: 2Generation 2:abbclength: 4Position to Binary Mapping:Position (k) | 0-indexed | Binary | 1-bits count | Shifts | Character 1 | 0 | 000 | 0 | 0 | a 2 | 1 | 001 | 1 | 1 | b 3 | 2 | 010 | 1 | 1 | b 4 | 3 | 011 | 2 | 2 | c 5 | 4 | 100 | 1 | 1 | b🎯 Key InsightThe number of 1-bits in binary(k-1) equals the number of alphabet transformations!This allows us to find any character in O(log k) time without building the string.
Understanding the Visualization
1
Recognize the Pattern
String doubles each operation, following powers of 2
2
Binary Position Mapping
Each bit position corresponds to a generation of transformation
3
Count Transformations
Number of 1-bits = number of alphabet shifts applied
Key Takeaway
🎯 Key Insight: The binary representation of position k-1 directly tells us how many transformations to apply to 'a'!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
24.3K Views
Medium Frequency
~15 min Avg. Time
892 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