Show problems as
Classic problem #3304EasyMathBit ManipulationRecursionSimulation

Find the K-th Character in String Game I

Real-world version: Letter at a Position in the Growing Tag
Tracked in this browser only
Read Find the K-th Character in String Game I on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A label generator keeps one word, which starts out as the single letter "a".

In one round the generator takes the word it currently holds, makes a copy in which every letter is replaced by the next letter of the alphabet, so 'a' becomes 'b', 'b' becomes 'c' and so on, and then sticks that copy on the end of the word. The word therefore doubles in length each round: after the first round it reads "ab", after the second "abbc".

Rounds keep running until the word holds at least k letters. Return the k-th letter of the word, counting the first letter as position 1. The bound on k keeps every letter within the alphabet, so no letter ever has to step past 'z'.

Examples

Example 1

Input
k = 4
Output
"c"

After two rounds the word reads `"abbc"`, which already holds four letters, and its fourth letter is `"c"`.

Example 2

Input
k = 8
Output
"d"

After three rounds the word reads `"abbcbccd"`, and its eighth letter is `"d"`.

Example 3

Input
k = 12
Output
"d"

Four rounds give `"abbcbccdbccdcdde"`. Its twelfth letter is `"d"`.

Constraints

  • 1 <= k <= 500

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def kth_character(k: int) -> str:
Java
public char kthCharacter(int k)