Skip to main content

Command Palette

Search for a command to run...

Find the K-th Character in String Game I

Published
2 min read
Find the K-th Character in String Game I
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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 k<sup>th</sup> 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.

LeetCode Problem - 3304

class Solution {

    // Method to find the k-th character in a string generated by repeating a transformation process starting from "a"
    public char kthCharacter(int k) {
        String start = "a";  // Initial string is "a"
        if (k == 1) return 'a';  // If k is 1, return 'a' as it's the first character

        char ans = '1';  // Initialize the result character

        // Continuously create new strings until the length of the string is at least k
        while (true) {
            start = createNewString(start);  // Generate the new string by transforming the current string

            // If the newly generated string is long enough, extract the k-th character
            if (start.length() >= k) {
                ans = start.charAt(k - 1);  // Get the k-th character (0-based index)
                break;  // Exit the loop once the answer is found
            }
        }

        return ans;  // Return the k-th character
    }

    // Helper method to create a new string by transforming the given string
    public String createNewString(String str) {
        StringBuilder sb = new StringBuilder(str);  // Use StringBuilder for efficient string concatenation

        // Iterate over each character in the original string
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);  // Get the current character

            // If the character is 'z', append 'a' to the new string, otherwise append the next character
            if (ch == 'z') {
                sb.append('a');
            } else {
                sb.append((char)(ch + 1));  // Increment the character to the next alphabet letter
            }
        }

        return sb.toString();  // Return the newly generated string
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.