Skip to content

Add solution and test-cases for problem 1209 #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# [1209.Remove All Adjacent Duplicates in String II][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

We repeatedly make `k` **duplicate removals** on `s` until we no longer can.

Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is **unique**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "abcd", k = 2
Output: "abcd"
Explanation: There's nothing to delete.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Remove All Adjacent Duplicates in String II
```go
```
Input: s = "deeedbbcccbdaa", k = 3
Output: "aa"
Explanation:
First delete "eee" and "ccc", get "ddbbbdaa"
Then delete "bbb", get "dddaa"
Finally delete "ddd", get "aa"
```

**Example 3:**

```
Input: s = "pbbcggttciiippooaais", k = 2
Output: "ps"
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

type item1209 struct {
b byte
c int
}

func Solution(s string, k int) string {
stack := make([]item1209, len(s))
index := -1
for i := 0; i < len(s); i++ {
if index == -1 {
index++
stack[index] = item1209{b: s[i], c: 1}
continue
}
if s[i] == stack[index].b {
stack[index].c++
if stack[index].c == k {
index--
}
continue
}
index++
stack[index] = item1209{b: s[i], c: 1}
}
buf := strings.Builder{}
for i := 0; i <= index; i++ {
for ; stack[i].c > 0; stack[i].c-- {
buf.WriteByte(stack[i].b)
}
}

return buf.String()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
k int
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "abcd", 2, "abcd"},
{"TestCase2", "deeedbbcccbdaa", 3, "aa"},
{"TestCase3", "pbbcggttciiippooaais", 2, "ps"},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading