Talentd
FleetCode

3304. Find the K-th Character in String Game I

Easy81.6% acceptance5 companies

Alice and Bob are playing a game. Initially, Alice has a string word = "a".

You are given a positive integer k.

Now Bob will ask Alice to perform the following operation forever:

  • Generate a new string by changing each character in word to its next character in the English alphabet, and append it to the original word.

For example, performing the operation on "c" generates "cd" and performing the operation on "zb" generates "zbac".

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

Note that the character 'z' can be changed to 'a' in the operation.

Example 1:

Input: k = 5

Output: "b"

Explanation:

Initially, word = "a". We need to do the operation three times:

  • Generated string is "b", word becomes "ab".
  • Generated string is "bc", word becomes "abbc".
  • Generated string is "bccd", word becomes "abbcbccd".

Example 2:

Input: k = 10

Output: "c"

Constraints:

  • 1 <= k <= 500

Hints open one at a time

Hint 1
The constraints are small. Construct the string by simulating the operations.
17:39
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution

Similar problems

Shifting LettersMedium
Loading editor…
1class Solution:
2 def kthCharacter(self, k: int) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
5
Input
10
Your code is saved on this device.Solution