3601. Find The K Th Character In String Game Ii¶
Difficulty: Hard
LeetCode Problem View on GitHub
3601. Find the K-th Character in String Game II
Hard
Alice and Bob are playing a game. Initially, Alice has a string word = "a".
You are given a positive integer k. You are also given an integer array operations, where operations[i] represents the type of the ith operation.
Now Bob will ask Alice to perform all operations in sequence:
- If
operations[i] == 0, append a copy ofwordto itself. - If
operations[i] == 1, generate a new string by changing each character inwordto its next character in the English alphabet, and append it to the originalword. 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 performing all the operations.
Note that the character 'z' can be changed to 'a' in the second type of operation.
Example 1:
Input: k = 5, operations = [0,0,0]
Output: "a"
Explanation:
Initially, word == "a". Alice performs the three operations as follows:
- Appends
"a"to"a",wordbecomes"aa". - Appends
"aa"to"aa",wordbecomes"aaaa". - Appends
"aaaa"to"aaaa",wordbecomes"aaaaaaaa".
Example 2:
Input: k = 10, operations = [0,1,0,1]
Output: "b"
Explanation:
Initially, word == "a". Alice performs the four operations as follows:
- Appends
"a"to"a",wordbecomes"aa". - Appends
"bb"to"aa",wordbecomes"aabb". - Appends
"aabb"to"aabb",wordbecomes"aabbaabb". - Appends
"bbccbbcc"to"aabbaabb",wordbecomes"aabbaabbbbccbbcc".
Constraints:
1 <= k <= 10141 <= operations.length <= 100operations[i]is either 0 or 1.- The input is generated such that
wordhas at leastkcharacters after all operations.
Solution¶
class Solution {
public char kthCharacter(long k, int[] arr) {
int res = 0, c = 0, idx = 0;
k--;
while (idx < arr.length && k > 0) {
c += ((int)(k & 1) & arr[idx]);
k >>= 1;
idx++;
}
return (char)((c % 26) + 'a');
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here