Skip to content

Commit 882870c

Browse files
authored
feat: add solutions to lc problem: No.3307 (#3584)
No.3307.Find the K-th Character in String Game II
1 parent 3a72261 commit 882870c

File tree

7 files changed

+311
-8
lines changed

7 files changed

+311
-8
lines changed

solution/3300-3399/3307.Find the K-th Character in String Game II/README.md

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3307.Fi
8686

8787
<!-- solution:start -->
8888

89-
### 方法一
89+
### 方法一:递推
90+
91+
由于每次操作后,字符串的长度都会翻倍,因此,如果进行 $i$ 次操作,字符串的长度将会是 $2^i$。
92+
93+
我们可以模拟这个过程,找到第一个大于等于 $k$ 的字符串长度 $n$。
94+
95+
接下来,我们再往回推,分情况讨论:
96+
97+
- 如果 $k \gt n / 2$,说明 $k$ 在后半部分,如果此时 $\textit{operations}[i - 1] = 1$,说明 $k$ 所在的字符是由前半部分的字符加上 $1$ 得到的,我们加上 $1$。然后我们更新 $k$ 为 $k - n / 2$。
98+
- 如果 $k \le n / 2$,说明 $k$ 在前半部分,不会受到 $\textit{operations}[i - 1]$ 的影响。
99+
- 接下来,我们更新 $n$ 为 $n / 2$,继续往前推,直到 $n = 1$。
100+
101+
最后,我们将得到的数字对 $26$ 取模,加上 `'a'` 的 ASCII 码,即可得到答案。
102+
103+
时间复杂度 $O(\log k)$,空间复杂度 $O(1)$。
90104

91105
<!-- tabs:start -->
92106

93107
#### Python3
94108

95109
```python
96-
110+
class Solution:
111+
def kthCharacter(self, k: int, operations: List[int]) -> str:
112+
n, i = 1, 0
113+
while n < k:
114+
n *= 2
115+
i += 1
116+
d = 0
117+
while n > 1:
118+
if k > n // 2:
119+
k -= n // 2
120+
d += operations[i - 1]
121+
n //= 2
122+
i -= 1
123+
return chr(d % 26 + ord("a"))
97124
```
98125

99126
#### Java
100127

101128
```java
102-
129+
class Solution {
130+
public char kthCharacter(long k, int[] operations) {
131+
long n = 1;
132+
int i = 0;
133+
while (n < k) {
134+
n *= 2;
135+
++i;
136+
}
137+
int d = 0;
138+
while (n > 1) {
139+
if (k > n / 2) {
140+
k -= n / 2;
141+
d += operations[i - 1];
142+
}
143+
n /= 2;
144+
--i;
145+
}
146+
return (char) ('a' + (d % 26));
147+
}
148+
}
103149
```
104150

105151
#### C++
106152

107153
```cpp
108-
154+
class Solution {
155+
public:
156+
char kthCharacter(long long k, vector<int>& operations) {
157+
long long n = 1;
158+
int i = 0;
159+
while (n < k) {
160+
n *= 2;
161+
++i;
162+
}
163+
int d = 0;
164+
while (n > 1) {
165+
if (k > n / 2) {
166+
k -= n / 2;
167+
d += operations[i - 1];
168+
}
169+
n /= 2;
170+
--i;
171+
}
172+
return 'a' + (d % 26);
173+
}
174+
};
109175
```
110176
111177
#### Go
112178
113179
```go
180+
func kthCharacter(k int64, operations []int) byte {
181+
n := int64(1)
182+
i := 0
183+
for n < k {
184+
n *= 2
185+
i++
186+
}
187+
d := 0
188+
for n > 1 {
189+
if k > n/2 {
190+
k -= n / 2
191+
d += operations[i-1]
192+
}
193+
n /= 2
194+
i--
195+
}
196+
return byte('a' + (d % 26))
197+
}
198+
```
114199

200+
#### TypeScript
201+
202+
```ts
203+
function kthCharacter(k: number, operations: number[]): string {
204+
let n = 1;
205+
let i = 0;
206+
while (n < k) {
207+
n *= 2;
208+
i++;
209+
}
210+
let d = 0;
211+
while (n > 1) {
212+
if (k > n / 2) {
213+
k -= n / 2;
214+
d += operations[i - 1];
215+
}
216+
n /= 2;
217+
i--;
218+
}
219+
return String.fromCharCode('a'.charCodeAt(0) + (d % 26));
220+
}
115221
```
116222

117223
<!-- tabs:end -->

solution/3300-3399/3307.Find the K-th Character in String Game II/README_EN.md

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3307.Fi
8383

8484
<!-- solution:start -->
8585

86-
### Solution 1
86+
### Solution 1: Recurrence
87+
88+
Since the length of the string doubles after each operation, if we perform $i$ operations, the length of the string will be $2^i$.
89+
90+
We can simulate this process to find the first string length $n$ that is greater than or equal to $k$.
91+
92+
Next, we backtrack and discuss the following cases:
93+
94+
- If $k \gt n / 2$, it means $k$ is in the second half. If $\textit{operations}[i - 1] = 1$, it means the character at position $k$ is obtained by adding $1$ to the character in the first half. We add $1$ to it. Then we update $k$ to $k - n / 2$.
95+
- If $k \le n / 2$, it means $k$ is in the first half and is not affected by $\textit{operations}[i - 1]$.
96+
- Next, we update $n$ to $n / 2$ and continue backtracking until $n = 1$.
97+
98+
Finally, we take the resulting number modulo $26$ and add the ASCII code of `'a'` to get the answer.
99+
100+
The time complexity is $O(\log k)$, and the space complexity is $O(1)$.
87101

88102
<!-- tabs:start -->
89103

90104
#### Python3
91105

92106
```python
93-
107+
class Solution:
108+
def kthCharacter(self, k: int, operations: List[int]) -> str:
109+
n, i = 1, 0
110+
while n < k:
111+
n *= 2
112+
i += 1
113+
d = 0
114+
while n > 1:
115+
if k > n // 2:
116+
k -= n // 2
117+
d += operations[i - 1]
118+
n //= 2
119+
i -= 1
120+
return chr(d % 26 + ord("a"))
94121
```
95122

96123
#### Java
97124

98125
```java
99-
126+
class Solution {
127+
public char kthCharacter(long k, int[] operations) {
128+
long n = 1;
129+
int i = 0;
130+
while (n < k) {
131+
n *= 2;
132+
++i;
133+
}
134+
int d = 0;
135+
while (n > 1) {
136+
if (k > n / 2) {
137+
k -= n / 2;
138+
d += operations[i - 1];
139+
}
140+
n /= 2;
141+
--i;
142+
}
143+
return (char) ('a' + (d % 26));
144+
}
145+
}
100146
```
101147

102148
#### C++
103149

104150
```cpp
105-
151+
class Solution {
152+
public:
153+
char kthCharacter(long long k, vector<int>& operations) {
154+
long long n = 1;
155+
int i = 0;
156+
while (n < k) {
157+
n *= 2;
158+
++i;
159+
}
160+
int d = 0;
161+
while (n > 1) {
162+
if (k > n / 2) {
163+
k -= n / 2;
164+
d += operations[i - 1];
165+
}
166+
n /= 2;
167+
--i;
168+
}
169+
return 'a' + (d % 26);
170+
}
171+
};
106172
```
107173
108174
#### Go
109175
110176
```go
177+
func kthCharacter(k int64, operations []int) byte {
178+
n := int64(1)
179+
i := 0
180+
for n < k {
181+
n *= 2
182+
i++
183+
}
184+
d := 0
185+
for n > 1 {
186+
if k > n/2 {
187+
k -= n / 2
188+
d += operations[i-1]
189+
}
190+
n /= 2
191+
i--
192+
}
193+
return byte('a' + (d % 26))
194+
}
195+
```
111196

197+
#### TypeScript
198+
199+
```ts
200+
function kthCharacter(k: number, operations: number[]): string {
201+
let n = 1;
202+
let i = 0;
203+
while (n < k) {
204+
n *= 2;
205+
i++;
206+
}
207+
let d = 0;
208+
while (n > 1) {
209+
if (k > n / 2) {
210+
k -= n / 2;
211+
d += operations[i - 1];
212+
}
213+
n /= 2;
214+
i--;
215+
}
216+
return String.fromCharCode('a'.charCodeAt(0) + (d % 26));
217+
}
112218
```
113219

114220
<!-- tabs:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
char kthCharacter(long long k, vector<int>& operations) {
4+
long long n = 1;
5+
int i = 0;
6+
while (n < k) {
7+
n *= 2;
8+
++i;
9+
}
10+
int d = 0;
11+
while (n > 1) {
12+
if (k > n / 2) {
13+
k -= n / 2;
14+
d += operations[i - 1];
15+
}
16+
n /= 2;
17+
--i;
18+
}
19+
return 'a' + (d % 26);
20+
}
21+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func kthCharacter(k int64, operations []int) byte {
2+
n := int64(1)
3+
i := 0
4+
for n < k {
5+
n *= 2
6+
i++
7+
}
8+
d := 0
9+
for n > 1 {
10+
if k > n/2 {
11+
k -= n / 2
12+
d += operations[i-1]
13+
}
14+
n /= 2
15+
i--
16+
}
17+
return byte('a' + (d % 26))
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public char kthCharacter(long k, int[] operations) {
3+
long n = 1;
4+
int i = 0;
5+
while (n < k) {
6+
n *= 2;
7+
++i;
8+
}
9+
int d = 0;
10+
while (n > 1) {
11+
if (k > n / 2) {
12+
k -= n / 2;
13+
d += operations[i - 1];
14+
}
15+
n /= 2;
16+
--i;
17+
}
18+
return (char) ('a' + (d % 26));
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def kthCharacter(self, k: int, operations: List[int]) -> str:
3+
n, i = 1, 0
4+
while n < k:
5+
n *= 2
6+
i += 1
7+
d = 0
8+
while n > 1:
9+
if k > n // 2:
10+
k -= n // 2
11+
d += operations[i - 1]
12+
n //= 2
13+
i -= 1
14+
return chr(d % 26 + ord("a"))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function kthCharacter(k: number, operations: number[]): string {
2+
let n = 1;
3+
let i = 0;
4+
while (n < k) {
5+
n *= 2;
6+
i++;
7+
}
8+
let d = 0;
9+
while (n > 1) {
10+
if (k > n / 2) {
11+
k -= n / 2;
12+
d += operations[i - 1];
13+
}
14+
n /= 2;
15+
i--;
16+
}
17+
return String.fromCharCode('a'.charCodeAt(0) + (d % 26));
18+
}

0 commit comments

Comments
 (0)