Skip to content

Commit c9bc31c

Browse files
committed
Add solution and test-cases for problem 1209
1 parent f0a9eb1 commit c9bc31c

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed

leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [1209.Remove All Adjacent Duplicates in String 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+
You are given a string `s` and an integer `k`, a `k` **duplicate removal** consists of choosing `k` adjacent and equal letters from `s` and removing them, causing the left and the right side of the deleted substring to concatenate together.
5+
6+
We repeatedly make `k` **duplicate removals** on `s` until we no longer can.
7+
8+
Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is **unique**.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "abcd", k = 2
14+
Output: "abcd"
15+
Explanation: There's nothing to delete.
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Remove All Adjacent Duplicates in String II
23-
```go
2420
```
21+
Input: s = "deeedbbcccbdaa", k = 3
22+
Output: "aa"
23+
Explanation:
24+
First delete "eee" and "ccc", get "ddbbbdaa"
25+
Then delete "bbb", get "dddaa"
26+
Finally delete "ddd", get "aa"
27+
```
28+
29+
**Example 3:**
2530

31+
```
32+
Input: s = "pbbcggttciiippooaais", k = 2
33+
Output: "ps"
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
type item1209 struct {
6+
b byte
7+
c int
8+
}
9+
10+
func Solution(s string, k int) string {
11+
stack := make([]item1209, len(s))
12+
index := -1
13+
for i := 0; i < len(s); i++ {
14+
if index == -1 {
15+
index++
16+
stack[index] = item1209{b: s[i], c: 1}
17+
continue
18+
}
19+
if s[i] == stack[index].b {
20+
stack[index].c++
21+
if stack[index].c == k {
22+
index--
23+
}
24+
continue
25+
}
26+
index++
27+
stack[index] = item1209{b: s[i], c: 1}
28+
}
29+
buf := strings.Builder{}
30+
for i := 0; i <= index; i++ {
31+
for ; stack[i].c > 0; stack[i].c-- {
32+
buf.WriteByte(stack[i].b)
33+
}
34+
}
35+
36+
return buf.String()
537
}

leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
k int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "abcd", 2, "abcd"},
18+
{"TestCase2", "deeedbbcccbdaa", 3, "aa"},
19+
{"TestCase3", "pbbcggttciiippooaais", 2, "ps"},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)