Find the K-th Character in String Game I
Real-world version: Letter at a Position in the Growing TagThe 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.
def kth_character(k: int) -> str:public char kthCharacter(int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 17 test cases: 17 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's