Skip to content

Commit 366eff5

Browse files
authored
Merge pull request #1251 from 0xff-dev/3307
Add solution and test-cases for problem 3307
2 parents 208de90 + 309a77f commit 366eff5

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/README.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
# [3307.Find the K-th Character in String Game II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Alice and Bob are playing a game. Initially, Alice has a string `word = "a"`.
5+
6+
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.
7+
8+
Now Bob will ask Alice to perform **all** operations in sequence:
9+
10+
- If `operations[i] == 0`, **append** a copy of `word` to itself.
11+
- If `operations[i] == 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"`.
12+
13+
Return the value of the `kth` character in `word` after performing all the operations.
14+
15+
**Note** that the character `'z'` can be changed to `'a'` in the second type of operation.
716

817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
20+
Input: k = 5, operations = [0,0,0]
21+
22+
Output: "a"
1423
15-
## 题意
16-
> ...
24+
Explanation:
1725
18-
## 题解
26+
Initially, word == "a". Alice performs the three operations as follows:
1927
20-
### 思路1
21-
> ...
22-
Find the K-th Character in String Game II
23-
```go
28+
Appends "a" to "a", word becomes "aa".
29+
Appends "aa" to "aa", word becomes "aaaa".
30+
Appends "aaaa" to "aaaa", word becomes "aaaaaaaa".
2431
```
2532

33+
**Example 2:**
34+
35+
```
36+
Input: k = 10, operations = [0,1,0,1]
37+
38+
Output: "b"
39+
40+
Explanation:
41+
42+
Initially, word == "a". Alice performs the four operations as follows:
43+
44+
Appends "a" to "a", word becomes "aa".
45+
Appends "bb" to "aa", word becomes "aabb".
46+
Appends "aabb" to "aabb", word becomes "aabbaabb".
47+
Appends "bbccbbcc" to "aabbaabb", word becomes "aabbaabbbbccbbcc".
48+
```
2649

2750
## 结语
2851

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "math/bits"
4+
5+
func Solution(k int64, operations []int) byte {
6+
ans := 0
7+
for k != 1 {
8+
t := bits.Len64(uint64(k)) - 1
9+
if (1 << t) == k {
10+
t--
11+
}
12+
k -= (1 << t)
13+
if operations[t] != 0 {
14+
ans++
15+
}
16+
}
17+
return byte('a' + (ans % 26))
518
}

leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
k int64
14+
operations []int
15+
expect byte
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 5, []int{0, 0, 0}, 'a'},
18+
{"TestCase2", 10, []int{0, 1, 0, 1}, 'b'},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.k, c.operations)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.k, c.operations)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)