Skip to content

Commit 7d9cae5

Browse files
committed
Add solution and test-cases for problem 3304
1 parent 8ae5d85 commit 7d9cae5

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [3304.Find the K-th Character in String Game I][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`.
7+
8+
Now Bob will ask Alice to perform the following operation **forever**:
9+
10+
- 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`.
11+
12+
For example, performing the operation on `"c"` generates `"cd"` and performing the operation on `"zb"` generates `"zbac"`.
13+
14+
Return the value of the `kth` character in `word`, after enough operations have been done for `word` to have **at least** `k` characters.
15+
16+
**Note** that the character `'z'` can be changed to `'a'` in the operation.
717

818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
21+
Input: k = 5
22+
23+
Output: "b"
24+
25+
Explanation:
1426
15-
## 题意
16-
> ...
27+
Initially, word = "a". We need to do the operation three times:
1728
18-
## 题解
29+
Generated string is "b", word becomes "ab".
30+
Generated string is "bc", word becomes "abbc".
31+
Generated string is "bccd", word becomes "abbcbccd".
32+
```
33+
34+
**Example 2:**
1935

20-
### 思路1
21-
> ...
22-
Find the K-th Character in String Game I
23-
```go
2436
```
37+
Input: k = 10
2538
39+
Output: "c"
40+
```
2641

2742
## 结语
2843

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(k int) byte {
4+
str := "a"
5+
for len(str) < k {
6+
tmp := []byte(str)
7+
for i := range tmp {
8+
tmp[i] = (tmp[i]-'a'+1)%26 + 'a'
9+
}
10+
str += string(tmp)
11+
}
12+
return str[k-1]
513
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect byte
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 5, 'b'},
17+
{"TestCase2", 10, 'c'},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)