LeetCode 3304: Find the K-th Character in String Game I Solution

Master LeetCode problem 3304 (Find the K-th Character in String Game I), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

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

Problem Explanation

Explanation

To solve this problem, we can simulate the process of generating new strings by changing each character to its next character in the English alphabet until the word has at least k characters. We can keep track of the current index in the generated string and update it accordingly. Finally, we return the k-th character in the word.

  • We initialize the word as "a" and the current index as 1.
  • We iterate from 1 to k, generating new strings by changing each character to its next character and appending it to the original word.
  • We update the current index based on the length of the generated string.
  • Finally, we return the k-th character in the word.

Time complexity: O(k) Space complexity: O(k)

Solution Code

class Solution {
    public char findKthCharacter(int k) {
        String word = "a";
        int currentIndex = 1;
        
        for (int i = 1; i < k; i++) {
            char nextChar = (char)((word.charAt(currentIndex - 1) - 'a' + 1) % 26 + 'a');
            word += nextChar;
            currentIndex = word.length();
        }
        
        return word.charAt(k - 1);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3304 (Find the K-th Character in String Game I)?

This page provides optimized solutions for LeetCode problem 3304 (Find the K-th Character in String Game I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3304 (Find the K-th Character in String Game I)?

The time complexity for LeetCode 3304 (Find the K-th Character in String Game I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3304 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3304 in Java, C++, or Python.

Back to LeetCode Solutions