Skip to content

Commit 18e6d08

Browse files
committed
Add solution 1209
1 parent 09463ee commit 18e6d08

20 files changed

+454
-129
lines changed

README.md

Lines changed: 104 additions & 104 deletions
Large diffs are not rendered by default.

leetcode/1074.Number-of-Submatrices-That-Sum-to-Target/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Two submatrices `(x1, y1, x2, y2)` and `(x1', y1', x2', y2')` are different
1111

1212
**Example 1:**
1313

14+
![](https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg)
15+
1416
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
1517
Output: 4
1618
Explanation: The four 1x1 submatrices that only contain 0.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
// 解法一 stack
4+
func removeDuplicates(s string, k int) string {
5+
stack, arr := [][2]int{}, []byte{}
6+
for _, c := range s {
7+
i := int(c - 'a')
8+
if len(stack) > 0 && stack[len(stack)-1][0] == i {
9+
stack[len(stack)-1][1]++
10+
if stack[len(stack)-1][1] == k {
11+
stack = stack[:len(stack)-1]
12+
}
13+
} else {
14+
stack = append(stack, [2]int{i, 1})
15+
}
16+
}
17+
for _, pair := range stack {
18+
c := byte(pair[0] + 'a')
19+
for i := 0; i < pair[1]; i++ {
20+
arr = append(arr, c)
21+
}
22+
}
23+
return string(arr)
24+
}
25+
26+
// 解法二 暴力
27+
func removeDuplicates1(s string, k int) string {
28+
arr, count, tmp := []rune{}, 0, '#'
29+
for _, v := range s {
30+
arr = append(arr, v)
31+
for len(arr) > 0 {
32+
count = 0
33+
tmp = arr[len(arr)-1]
34+
for i := len(arr) - 1; i >= 0; i-- {
35+
if arr[i] != tmp {
36+
break
37+
}
38+
count++
39+
}
40+
if count == k {
41+
arr = arr[:len(arr)-k]
42+
} else {
43+
break
44+
}
45+
}
46+
}
47+
return string(arr)
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1209 struct {
9+
para1209
10+
ans1209
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1209 struct {
16+
s string
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1209 struct {
23+
one string
24+
}
25+
26+
func Test_Problem1209(t *testing.T) {
27+
28+
qs := []question1209{
29+
30+
// {
31+
// para1209{"abcd", 2},
32+
// ans1209{"abcd"},
33+
// },
34+
35+
{
36+
para1209{"deeedbbcccbdaa", 3},
37+
ans1209{"aa"},
38+
},
39+
40+
{
41+
para1209{"pbbcggttciiippooaais", 2},
42+
ans1209{"ps"},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 1209------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans1209, q.para1209
50+
fmt.Printf("【input】:%v 【output】:%v\n", p, removeDuplicates(p.s, p.k))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# [1209. Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/)
2+
3+
4+
## 题目
5+
6+
Given a string `s`, 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.
7+
8+
We repeatedly make `k` duplicate removals on `s` until we no longer can.
9+
10+
Return the final string after all such duplicate removals have been made.
11+
12+
It is guaranteed that the answer is unique.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: s = "abcd", k = 2
18+
Output: "abcd"
19+
Explanation:There's nothing to delete.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: s = "deeedbbcccbdaa", k = 3
26+
Output: "aa"
27+
Explanation:
28+
First delete "eee" and "ccc", get "ddbbbdaa"
29+
Then delete "bbb", get "dddaa"
30+
Finally delete "ddd", get "aa"
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: s = "pbbcggttciiippooaais", k = 2
37+
Output: "ps"
38+
```
39+
40+
**Constraints:**
41+
42+
- `1 <= s.length <= 10^5`
43+
- `2 <= k <= 10^4`
44+
- `s` only contains lower case English letters.
45+
46+
## 题目大意
47+
48+
给你一个字符串 s,「k 倍重复项删除操作」将会从 s 中选择 k 个相邻且相等的字母,并删除它们,使被删去的字符串的左侧和右侧连在一起。你需要对 s 重复进行无限次这样的删除操作,直到无法继续为止。在执行完所有删除操作后,返回最终得到的字符串。本题答案保证唯一。
49+
50+
## 解题思路
51+
52+
- 暴力解法。每增加一个字符,就往前扫描 `k` 位,判断是否存在 `k` 个连续相同的字符。消除了 `k` 个相同字符后,重新组成的字符串还可能再次产出 `k` 位相同的字符,(类似消消乐,`k` 个相同的字符碰到一起就“消除”),还需要继续消除。最差情况要再次扫描一次字符串。时间复杂度 O(n^2),空间复杂度 O(n)。
53+
- 暴力解法的低效在于重复统计字符频次,如果每个字符的频次统计一次就好了。按照这个思路,利用 stack ,每个栈元素存 2 个值,一个是字符,一个是该字符对应的频次。有了栈顶字符频次信息,就不需要重复往前扫描了。只要栈顶字符频次到达了 `k`,就弹出该字符。如此反复,最终剩下的字符串为所求。时间复杂度 O(n),空间复杂度 O(n)。
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
// 解法一 stack
61+
func removeDuplicates(s string, k int) string {
62+
stack, arr := [][2]int{}, []byte{}
63+
for _, c := range s {
64+
i := int(c - 'a')
65+
if len(stack) > 0 && stack[len(stack)-1][0] == i {
66+
stack[len(stack)-1][1]++
67+
if stack[len(stack)-1][1] == k {
68+
stack = stack[:len(stack)-1]
69+
}
70+
} else {
71+
stack = append(stack, [2]int{i, 1})
72+
}
73+
}
74+
for _, pair := range stack {
75+
c := byte(pair[0] + 'a')
76+
for i := 0; i < pair[1]; i++ {
77+
arr = append(arr, c)
78+
}
79+
}
80+
return string(arr)
81+
}
82+
83+
// 解法二 暴力
84+
func removeDuplicates1(s string, k int) string {
85+
arr, count, tmp := []rune{}, 0, '#'
86+
for _, v := range s {
87+
arr = append(arr, v)
88+
for len(arr) > 0 {
89+
count = 0
90+
tmp = arr[len(arr)-1]
91+
for i := len(arr) - 1; i >= 0; i-- {
92+
if arr[i] != tmp {
93+
break
94+
}
95+
count++
96+
}
97+
if count == k {
98+
arr = arr[:len(arr)-k]
99+
} else {
100+
break
101+
}
102+
}
103+
}
104+
return string(arr)
105+
}
106+
```

website/content/ChapterFour/1000~1099/1074.Number-of-Submatrices-That-Sum-to-Target.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Two submatrices `(x1, y1, x2, y2)` and `(x1', y1', x2', y2')` are different
1111

1212
**Example 1**:
1313

14+
![](https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg)
15+
1416
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
1517
Output: 4
1618
Explanation: The four 1x1 submatrices that only contain 0.

website/content/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ func equalSubstring(s string, t string, maxCost int) int {
8080
----------------------------------------------
8181
<div style="display: flex;justify-content: space-between;align-items: center;">
8282
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1207.Unique-Number-of-Occurrences/">⬅️上一页</a></p>
83-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position/">下一页➡️</a></p>
83+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1209.Remove-All-Adjacent-Duplicates-in-String-II/">下一页➡️</a></p>
8484
</div>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# [1209. Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/)
2+
3+
4+
## 题目
5+
6+
Given a string `s`, 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.
7+
8+
We repeatedly make `k` duplicate removals on `s` until we no longer can.
9+
10+
Return the final string after all such duplicate removals have been made.
11+
12+
It is guaranteed that the answer is unique.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: s = "abcd", k = 2
18+
Output: "abcd"
19+
Explanation:There's nothing to delete.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: s = "deeedbbcccbdaa", k = 3
26+
Output: "aa"
27+
Explanation:
28+
First delete "eee" and "ccc", get "ddbbbdaa"
29+
Then delete "bbb", get "dddaa"
30+
Finally delete "ddd", get "aa"
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: s = "pbbcggttciiippooaais", k = 2
37+
Output: "ps"
38+
```
39+
40+
**Constraints:**
41+
42+
- `1 <= s.length <= 10^5`
43+
- `2 <= k <= 10^4`
44+
- `s` only contains lower case English letters.
45+
46+
## 题目大意
47+
48+
给你一个字符串 s,「k 倍重复项删除操作」将会从 s 中选择 k 个相邻且相等的字母,并删除它们,使被删去的字符串的左侧和右侧连在一起。你需要对 s 重复进行无限次这样的删除操作,直到无法继续为止。在执行完所有删除操作后,返回最终得到的字符串。本题答案保证唯一。
49+
50+
## 解题思路
51+
52+
- 暴力解法。每增加一个字符,就往前扫描 `k` 位,判断是否存在 `k` 个连续相同的字符。消除了 `k` 个相同字符后,重新组成的字符串还可能再次产出 `k` 位相同的字符,(类似消消乐,`k` 个相同的字符碰到一起就“消除”),还需要继续消除。最差情况要再次扫描一次字符串。时间复杂度 O(n^2),空间复杂度 O(n)。
53+
- 暴力解法的低效在于重复统计字符频次,如果每个字符的频次统计一次就好了。按照这个思路,利用 stack ,每个栈元素存 2 个值,一个是字符,一个是该字符对应的频次。有了栈顶字符频次信息,就不需要重复往前扫描了。只要栈顶字符频次到达了 `k`,就弹出该字符。如此反复,最终剩下的字符串为所求。时间复杂度 O(n),空间复杂度 O(n)。
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
// 解法一 stack
61+
func removeDuplicates(s string, k int) string {
62+
stack, arr := [][2]int{}, []byte{}
63+
for _, c := range s {
64+
i := int(c - 'a')
65+
if len(stack) > 0 && stack[len(stack)-1][0] == i {
66+
stack[len(stack)-1][1]++
67+
if stack[len(stack)-1][1] == k {
68+
stack = stack[:len(stack)-1]
69+
}
70+
} else {
71+
stack = append(stack, [2]int{i, 1})
72+
}
73+
}
74+
for _, pair := range stack {
75+
c := byte(pair[0] + 'a')
76+
for i := 0; i < pair[1]; i++ {
77+
arr = append(arr, c)
78+
}
79+
}
80+
return string(arr)
81+
}
82+
83+
// 解法二 暴力
84+
func removeDuplicates1(s string, k int) string {
85+
arr, count, tmp := []rune{}, 0, '#'
86+
for _, v := range s {
87+
arr = append(arr, v)
88+
for len(arr) > 0 {
89+
count = 0
90+
tmp = arr[len(arr)-1]
91+
for i := len(arr) - 1; i >= 0; i-- {
92+
if arr[i] != tmp {
93+
break
94+
}
95+
count++
96+
}
97+
if count == k {
98+
arr = arr[:len(arr)-k]
99+
} else {
100+
break
101+
}
102+
}
103+
}
104+
return string(arr)
105+
}
106+
```
107+
108+
109+
----------------------------------------------
110+
<div style="display: flex;justify-content: space-between;align-items: center;">
111+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget/">⬅️上一页</a></p>
112+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position/">下一页➡️</a></p>
113+
</div>

website/content/ChapterFour/1200~1299/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ func minCostToMoveChips(chips []int) int {
7878

7979
----------------------------------------------
8080
<div style="display: flex;justify-content: space-between;align-items: center;">
81-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget/">⬅️上一页</a></p>
81+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1209.Remove-All-Adjacent-Duplicates-in-String-II/">⬅️上一页</a></p>
8282
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1200~1299/1221.Split-a-String-in-Balanced-Strings/">下一页➡️</a></p>
8383
</div>

0 commit comments

Comments
 (0)