Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created July 3, 2025 18:09
Show Gist options
  • Save Ifihan/9f96c66154c61cf2949d72634d7fd32b to your computer and use it in GitHub Desktop.
Save Ifihan/9f96c66154c61cf2949d72634d7fd32b to your computer and use it in GitHub Desktop.
Find the K-th Character in String Game I

Question

Approach

I start with the initial string word = "a". In each operation, I take the current string and generate a new string by replacing each character with its next character in the English alphabet—where 'z' wraps around to 'a'.

Once the length of the string reaches or exceeds k, I return the k-th character (1-indexed) from the resulting string.

Implementation

class Solution:
    def kthCharacter(self, k: int) -> str:
        word = "a"
        while len(word) < k:
            next_part = ''.join(
                chr(((ord(c) - ord('a') + 1) % 26) + ord('a')) for c in word
            )
            word += next_part
        return word[k - 1]

Complexities

  • Time: O(k)
  • Space: O(k)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment