Post

3600 Find The K Th Character In String Game I

3600 Find The K Th Character In String Game I

Find the K-th Character in String Game I image

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:

1
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:

1
2
3
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
1 <= k <= 500
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

impl Solution {
    pub fn kth_character(k: i32) -> char {
        let mut arr : [char; 5001]= ['a'; 5001];
        let mut curr = 1;
        while(curr < k) {
            let sl: Vec<char> = arr[0..curr as usize].to_vec();
            let mut i  = 0;
            for m in sl.iter() {
                if (*m  == 'z') {
                    arr[curr as usize+i] = 'a';
                } else {
                    arr[curr as usize+i] = ((*m as u8) + 1) as char;
                }
                i += 1
                
            }
            curr *= 2      
        }
        //println!("{:?}",&arr[0..=curr as usize]);
        return arr[(k - 1) as usize]

    }
}



This post is licensed under CC BY 4.0 by the author.