less than 1 minute read

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

Solutions

class Solution {
    public char kthCharacter(int k) {
        int round = 0;
        
        while (Math.pow(2, round) < k) {
            round++;
        }
        
        // k is in round .
        
        // construct the string
        String temp = "a";
        while(round >= 0) {
            temp = getString(temp);
            round--;
        }
        
        return temp.charAt(k - 1);
    }
    
    private String getString(String input) {
        char[] chars = input.toCharArray();
        for(int i = 0; i < chars.length; i++) {
            chars[i]++;
            if (chars[i] > 'z') {
                chars[i] = 'a';
            }
        }
        return new StringBuilder(input).append(chars).toString();
    }
}