Find the K-th Character in String Game I (LeetCode 3304): Solution and Explanation

Easy
Math
Bit Manipulation
Recursion
Simulation

The word starts as "a" and keeps doubling with a shifted copy; return its k-th character without building it.

Intuition

Each operation appends a copy of the word with every letter moved up by one. After the word reaches length 2^m, the next round makes word[i + 2^m] equal to word[i] + 1 for every i below 2^m.

So any position at or above 2^m (but below 2^(m+1)) is a shifted copy of the position 2^m to its left. Jumping back by that power of two costs one shift and lands on a smaller position.

Jumping back by the highest power of two is exactly removing the highest 1 bit. Repeating until the position is 0 removes every 1 bit, so the number of shifts is the number of 1 bits in k - 1.

Approach: Strip the highest bit, count the jumps

  1. Set pos = k - 1 and shifts = 0.
  2. While pos > 0, let half be the highest power of two not above pos, subtract it from pos, and add 1 to shifts.
  3. Return the letter 'a' + shifts % 26.

Step-by-step example

k = 5
  1. pos = 4 (binary 100). The word after two rounds is abbc, after three it is abbcbccd.
  2. pos 4 lies in [4, 8), the shifted copy of [0, 4). Jump back by 4 to pos 0, shifts = 1.
  3. pos is 0, the original "a".
  4. Move "a" up one letter: the answer is "b".

Code: Python, Java, C++

1class Solution:
2    def kthCharacter(self, k: int) -> str:
3        shifts = 0
4        pos = k - 1
5        while pos > 0:
6            half = 1 << (pos.bit_length() - 1)
7            pos -= half
8            shifts += 1
9        return chr(ord('a') + shifts % 26)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Built-in popcount O(1)
Strip the highest bit, count the jumps O(log k)
Simulate the game O(k)
Time

O(log k)

Each jump removes one bit of k - 1, and k - 1 has at most 9 bits for k up to 500.

Space

O(1)

Only pos and the shift counter are stored.

Other approaches

Simulate the game (time O(k), space O(k))

Build the word by repeated doubling until its length reaches k, then index it. With k at most 500 this is at most 512 characters, so it is fast here, but it does not scale.

Built-in popcount (time O(1), space O(1))

Return 'a' + popcount(k - 1) directly, using bin(k - 1).count('1'), Integer.bitCount, __builtin_popcount or bits.OnesCount.

Common mistakes

  • Using k instead of k - 1. Positions are 1-indexed in the statement, but the doubling pattern works on 0-based indices.
  • Forgetting the wrap from z to a. It cannot happen for k up to 500, but taking shifts % 26 keeps the code correct for larger k.
  • Subtracting the lowest bit and treating that as the source position. The copy relation holds for the highest bit; only the final count happens to match.

Frequently asked questions

Why does the number of 1 bits give the answer?

Each 1 bit of k - 1 marks one doubling round where the target landed in the shifted right half. Every such round adds exactly one to the letter, and rounds where it landed in the left half add nothing.

Does the order of the bits matter?

No. The count of shifts is all that matters, so counting 1 bits from low to high gives the same result as stripping the highest bit first.

What changes in String Game II?

There each round is either a plain copy or a shifted copy, chosen by an operations array, so a 1 bit adds a shift only when its round was the shifting kind.