Skip to content

Commit 2e7dd97

Browse files
committed
Add solution 1332
1 parent 11e38a6 commit 2e7dd97

25 files changed

+781
-532
lines changed

README.md

Lines changed: 384 additions & 380 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode
2+
3+
func removePalindromeSub(s string) int {
4+
if len(s) == 0 {
5+
return 0
6+
}
7+
for i := 0; i < len(s)/2; i++ {
8+
if s[i] != s[len(s)-1-i] {
9+
return 2
10+
}
11+
}
12+
return 1
13+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1332 struct {
9+
para1332
10+
ans1332
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1332 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1332 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1332(t *testing.T) {
26+
27+
qs := []question1332{
28+
29+
{
30+
para1332{"ababa"},
31+
ans1332{1},
32+
},
33+
34+
{
35+
para1332{"abb"},
36+
ans1332{2},
37+
},
38+
39+
{
40+
para1332{"baabb"},
41+
ans1332{2},
42+
},
43+
44+
{
45+
para1332{""},
46+
ans1332{0},
47+
},
48+
49+
{
50+
para1332{"bbaabaaa"},
51+
ans1332{2},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 1332------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans1332, q.para1332
59+
fmt.Printf("【input】:%v 【output】:%v\n", p, removePalindromeSub(p.s))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [1332. Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/)
2+
3+
4+
## 题目
5+
6+
Given a string `s` consisting only of letters `'a'` and `'b'`. In a single step you can remove one palindromic **subsequence** from `s`.
7+
8+
Return the minimum number of steps to make the given string empty.
9+
10+
A string is a subsequence of a given string, if it is generated by deleting some characters of a given string without changing its order.
11+
12+
A string is called palindrome if is one that reads the same backward as well as forward.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: s = "ababa"
18+
Output: 1
19+
Explanation: String is already palindrome
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: s = "abb"
26+
Output: 2
27+
Explanation: "abb" -> "bb" -> "".
28+
Remove palindromic subsequence "a" then "bb".
29+
```
30+
31+
**Example 3:**
32+
33+
```
34+
Input: s = "baabb"
35+
Output: 2
36+
Explanation: "baabb" -> "b" -> "".
37+
Remove palindromic subsequence "baab" then "b".
38+
```
39+
40+
**Example 4:**
41+
42+
```
43+
Input: s = ""
44+
Output: 0
45+
```
46+
47+
**Constraints:**
48+
49+
- `0 <= s.length <= 1000`
50+
- `s` only consists of letters 'a' and 'b'
51+
52+
## 题目大意
53+
54+
给你一个字符串 s,它仅由字母 'a' 和 'b' 组成。每一次删除操作都可以从 s 中删除一个回文 子序列。返回删除给定字符串中所有字符(字符串为空)的最小删除次数。
55+
56+
「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个子序列。
57+
58+
「回文」定义:如果一个字符串向后和向前读是一致的,那么这个字符串就是一个回文。
59+
60+
## 解题思路
61+
62+
- 笔者读完题以为是第 5 题的加强版。在字符串中每次都找到最长的回文子串删除,一直删除到找不到回文子串结束,删除的总次数 + 剩余字母数 = 最小删除次数。提交以后 `wrong answer` 了,在 `bbaabaaa` 这组测试用例出错了。如果按照找最长回文字符串的思路,先找到最长回文子串 `aabaa`,剩余 `bba`,还需要再删除 2 次,`bb``a`。总共删除次数是 3 。为什么出错误了呢?仔细再读题,题目中说的是子序列,这不是连续的,再加上这道题是 `easy` 难度,其实很简单。
63+
- 这道题的答案只可能是 0,1,2 。空串对应的 0 。如果有一个字母,单个字母可以构成回文,所以是 1,如果字符串长度大于等于 2,即 `a``b` 都有,第一步先删除所有 `a`,因为所有的 `a` 构成了回文子序列。第二步删除所有的 `b`,因为所有的 `b` 构成了回文子序列。经过这样两步,一定能删除所有字符。
64+
65+
## 代码
66+
67+
```go
68+
package leetcode
69+
70+
func removePalindromeSub(s string) int {
71+
if len(s) == 0 {
72+
return 0
73+
}
74+
for i := 0; i < len(s)/2; i++ {
75+
if s[i] != s[len(s)-1-i] {
76+
return 2
77+
}
78+
}
79+
return 1
80+
}
81+
```

website/content/ChapterFour/1300~1399/1329.Sort-the-Matrix-Diagonally.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ func diagonalSort(mat [][]int) [][]int {
6060
----------------------------------------------
6161
<div style="display: flex;justify-content: space-between;align-items: center;">
6262
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1319.Number-of-Operations-to-Make-Network-Connected/">⬅️上一页</a></p>
63-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1337.The-K-Weakest-Rows-in-a-Matrix/">下一页➡️</a></p>
63+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1332.Remove-Palindromic-Subsequences/">下一页➡️</a></p>
6464
</div>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [1332. Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/)
2+
3+
4+
## 题目
5+
6+
Given a string `s` consisting only of letters `'a'` and `'b'`. In a single step you can remove one palindromic **subsequence** from `s`.
7+
8+
Return the minimum number of steps to make the given string empty.
9+
10+
A string is a subsequence of a given string, if it is generated by deleting some characters of a given string without changing its order.
11+
12+
A string is called palindrome if is one that reads the same backward as well as forward.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: s = "ababa"
18+
Output: 1
19+
Explanation: String is already palindrome
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: s = "abb"
26+
Output: 2
27+
Explanation: "abb" -> "bb" -> "".
28+
Remove palindromic subsequence "a" then "bb".
29+
```
30+
31+
**Example 3:**
32+
33+
```
34+
Input: s = "baabb"
35+
Output: 2
36+
Explanation: "baabb" -> "b" -> "".
37+
Remove palindromic subsequence "baab" then "b".
38+
```
39+
40+
**Example 4:**
41+
42+
```
43+
Input: s = ""
44+
Output: 0
45+
```
46+
47+
**Constraints:**
48+
49+
- `0 <= s.length <= 1000`
50+
- `s` only consists of letters 'a' and 'b'
51+
52+
## 题目大意
53+
54+
给你一个字符串 s,它仅由字母 'a' 和 'b' 组成。每一次删除操作都可以从 s 中删除一个回文 子序列。返回删除给定字符串中所有字符(字符串为空)的最小删除次数。
55+
56+
「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个子序列。
57+
58+
「回文」定义:如果一个字符串向后和向前读是一致的,那么这个字符串就是一个回文。
59+
60+
## 解题思路
61+
62+
- 笔者读完题以为是第 5 题的加强版。在字符串中每次都找到最长的回文子串删除,一直删除到找不到回文子串结束,删除的总次数 + 剩余字母数 = 最小删除次数。提交以后 `wrong answer` 了,在 `bbaabaaa` 这组测试用例出错了。如果按照找最长回文字符串的思路,先找到最长回文子串 `aabaa`,剩余 `bba`,还需要再删除 2 次,`bb``a`。总共删除次数是 3 。为什么出错误了呢?仔细再读题,题目中说的是子序列,这不是连续的,再加上这道题是 `easy` 难度,其实很简单。
63+
- 这道题的答案只可能是 0,1,2 。空串对应的 0 。如果有一个字母,单个字母可以构成回文,所以是 1,如果字符串长度大于等于 2,即 `a``b` 都有,第一步先删除所有 `a`,因为所有的 `a` 构成了回文子序列。第二步删除所有的 `b`,因为所有的 `b` 构成了回文子序列。经过这样两步,一定能删除所有字符。
64+
65+
## 代码
66+
67+
```go
68+
package leetcode
69+
70+
func removePalindromeSub(s string) int {
71+
if len(s) == 0 {
72+
return 0
73+
}
74+
for i := 0; i < len(s)/2; i++ {
75+
if s[i] != s[len(s)-1-i] {
76+
return 2
77+
}
78+
}
79+
return 1
80+
}
81+
```
82+
83+
84+
----------------------------------------------
85+
<div style="display: flex;justify-content: space-between;align-items: center;">
86+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1329.Sort-the-Matrix-Diagonally/">⬅️上一页</a></p>
87+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1337.The-K-Weakest-Rows-in-a-Matrix/">下一页➡️</a></p>
88+
</div>

website/content/ChapterFour/1300~1399/1337.The-K-Weakest-Rows-in-a-Matrix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ func kWeakestRows(mat [][]int, k int) []int {
9292

9393
----------------------------------------------
9494
<div style="display: flex;justify-content: space-between;align-items: center;">
95-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1329.Sort-the-Matrix-Diagonally/">⬅️上一页</a></p>
95+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1332.Remove-Palindromic-Subsequences/">⬅️上一页</a></p>
9696
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1380.Lucky-Numbers-in-a-Matrix/">下一页➡️</a></p>
9797
</div>

0 commit comments

Comments
 (0)