[LeetCode 3307] Find the K-th Character in String Game II
LeetCode 3307 (Java)
[Find the K-th Character in String Game II] 문제 풀이
[LeetCode 3307] Find the K-th Character in String Game II
Description
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".
- Appends
- Initially,
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".
- Initially,
Constraints
1 <= k <= 10^141 <= operations.length <= 100operations[i]is either 0 or 1.- The input is generated such that
wordhas at leastkcharacters after all operations.
Hint
Hint 1
Try to replay the operations `k^th` character was part of.
Hint 2
The `k^th` character is only affected if it is present in the first half of the string.
Code
내 제출
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public char kthCharacter(long k, int[] operations) {
long n = 1;
int i = 0;
while (n < k) {
n *= 2;
++i;
}
int d = 0;
while (n > 1) {
if (k > n / 2) {
k -= n / 2;
d += operations[i - 1];
}
n /= 2;
--i;
}
return (char) ('a' + (d % 26));
}
}
| Runtime | Memory |
|---|---|
| 1 ms | 43.1 MB |
다른 풀이
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
29
30
31
32
33
34
35
class Solution {
public char kthCharacter(long k, int[] arr) {
int c = 0; k--;
for (int i = 0; k != 0; i++, k >>= 1)
{
c += ((int)(k & 1) & arr[i]);
}
return (char)((c % 26) + 'a');
}
}
/*class Solution {
public char kthCharacter(long k, int[] operations) {
String ans = "a";
for (int i = 0; i < operations.length; i++) {
if (operations[i] == 0) {
ans = ans + ans;
} else {
String res = "";
for (int j = 0; j < ans.length(); j++) {
res += (char)(ans.charAt(j) + 1);
}
ans = ans + res;
}
if (ans.length() >= k) {
return ans.charAt((int)k - 1);
}
}
return ans.charAt((int)k - 1);
}
}*/
Reference
- https://github.com/doocs/leetcode/blob/main/solution/3300-3399/3307.Find%20the%20K-th%20Character%20in%20String%20Game%20II/Solution.java
This post is licensed under CC BY 4.0 by the author.
