From c9bc31cbb2ee5503f6190b005a107fd3772e33c9 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 7 Apr 2025 09:29:40 +0800 Subject: [PATCH] Add solution and test-cases for problem 1209 --- .../README.md | 35 +++++++++++------- .../Solution.go | 36 +++++++++++++++++-- .../Solution_test.go | 21 +++++------ 3 files changed, 67 insertions(+), 25 deletions(-) diff --git a/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/README.md b/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/README.md index 5e5851f4e..1cc94b7db 100644 --- a/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/README.md +++ b/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/README.md @@ -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" +``` ## 结语 diff --git a/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution.go b/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution.go index d115ccf5e..76aa7e7e4 100644 --- a/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution.go +++ b/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution.go @@ -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() } diff --git a/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution_test.go b/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution_test.go index 14ff50eb4..5b39de035 100644 --- a/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution_test.go +++ b/leetcode/1201-1300/1209.Remove-All-Adjacent-Duplicates-in-String-II/Solution_test.go @@ -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() { }