Skip to content

Commit b1b3872

Browse files
committed
Add solution and test-cases for problem 3163
1 parent ffaca5d commit b1b3872

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

leetcode/3101-3200/3163.String-Compression-III/README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
# [3163.String Compression III][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+
Given a string `word`, compress it using the following algorithm:
5+
6+
- Begin with an empty string `comp`. While `word` is **not** empty, use the following operation:
7+
8+
- Remove a maximum length prefix of `word` made of a single character `c` repeating **at most** 9 times.
9+
- Append the length of the prefix followed by `c` to `comp`.
10+
11+
Return the string `comp`.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
16+
Input: word = "abcde"
17+
18+
Output: "1a1b1c1d1e"
1419
15-
## 题意
16-
> ...
20+
Explanation:
1721
18-
## 题解
22+
Initially, comp = "". Apply the operation 5 times, choosing "a", "b", "c", "d", and "e" as the prefix in each operation.
1923
20-
### 思路1
21-
> ...
22-
String Compression III
23-
```go
24+
For each prefix, append "1" followed by the character to comp.
2425
```
2526

27+
**Example 2:**
28+
29+
```
30+
Input: word = "aaaaaaaaaaaaaabb"
31+
32+
Output: "9a5a2b"
33+
34+
Explanation:
35+
36+
Initially, comp = "". Apply the operation 3 times, choosing "aaaaaaaaa", "aaaaa", and "bb" as the prefix in each operation.
37+
38+
For prefix "aaaaaaaaa", append "9" followed by "a" to comp.
39+
For prefix "aaaaa", append "5" followed by "a" to comp.
40+
For prefix "bb", append "2" followed by "b" to comp.
41+
```
2642

2743
## 结语
2844

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(word string) string {
6+
buf := strings.Builder{}
7+
count := uint8(1)
8+
bs := []byte(word)
9+
pre := bs[0]
10+
for i := 1; i < len(bs); i++ {
11+
b := bs[i]
12+
if b == pre {
13+
count++
14+
if count == 9 {
15+
buf.WriteByte(count + '0')
16+
buf.WriteByte(b)
17+
count = 0
18+
}
19+
continue
20+
}
21+
if count != 0 {
22+
buf.WriteByte(count + '0')
23+
buf.WriteByte(pre)
24+
}
25+
pre = b
26+
count = 1
27+
}
28+
if count != 0 {
29+
buf.WriteByte(count + '0')
30+
buf.WriteByte(pre)
31+
}
32+
return buf.String()
533
}

leetcode/3101-3200/3163.String-Compression-III/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 string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abcde", "1a1b1c1d1e"},
17+
{"TestCase2", "aaaaaaaaaaaaaabb", "9a5a2b"},
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)