Skip to content

Commit 71037a4

Browse files
author
lucifer
committed
feat: #1332
1 parent a68821b commit 71037a4

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
146146
- [0575.distribute-candies](./problems/575.distribute-candies.md)
147147
- [0874.walking-robot-simulation](./problems/874.walking-robot-simulation.md) 🆕
148148
- [1260.shift-2d-grid](./problems/1260.shift-2d-grid.md) 🆕
149+
- [1332.remove-palindromic-subsequences](./problems/1332.remove-palindromic-subsequences.md) 🆕
149150

150151
#### 中等难度
151152

@@ -261,7 +262,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
261262
- [0239.sliding-window-maximum](./problems/239.sliding-window-maximum.md)
262263
- [0295.find-median-from-data-stream](./problems/295.find-median-from-data-stream.md) 🆕
263264
- [0301.remove-invalid-parentheses](./problems/301.remove-invalid-parentheses.md)
264-
- [0335.self-crossing](./problems/335.self-crossing.md) 🆕
265+
- [0335.self-crossPing](./problems/335.self-crossing.md) 🆕
265266
- [0460.lfu-cache](./problems/460.lfu-cache.md) 🆕
266267
- [0493.reverse-pairs](./problems/493.reverse-pairs.md) 🆕
267268
- [1168.optimize-water-distribution-in-a-village](./problems/1168.optimize-water-distribution-in-a-village-cn.md) 🆕
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 题目地址(1332. 删除回文子序列)
2+
3+
https://leetcode-cn.com/problems/remove-palindromic-subsequences/
4+
5+
## 题目描述
6+
7+
```给你一个字符串 s,它仅由字母 'a' 和 'b' 组成。每一次删除操作都可以从 s 中删除一个回文 子序列。
8+
9+
返回删除给定字符串中所有字符(字符串为空)的最小删除次数。
10+
11+
「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个子序列。
12+
13+
「回文」定义:如果一个字符串向后和向前读是一致的,那么这个字符串就是一个回文。
14+
15+
 
16+
17+
示例 1:
18+
19+
输入:s = "ababa"
20+
输出:1
21+
解释:字符串本身就是回文序列,只需要删除一次。
22+
示例 2:
23+
24+
输入:s = "abb"
25+
输出:2
26+
解释:"abb" -> "bb" -> "".
27+
先删除回文子序列 "a",然后再删除 "bb"。
28+
示例 3:
29+
30+
输入:s = "baabb"
31+
输出:2
32+
解释:"baabb" -> "b" -> "".
33+
先删除回文子序列 "baab",然后再删除 "b"。
34+
示例 4:
35+
36+
输入:s = ""
37+
输出:0
38+
 
39+
40+
提示:
41+
42+
0 <= s.length <= 1000
43+
s 仅包含字母 'a'  和 'b'
44+
在真实的面试中遇到过这道题?
45+
```
46+
47+
## 思路
48+
49+
这又是一道“抖机灵”的题目,类似的题目有[1297.maximum-number-of-occurrences-of-a-substring](https://github.com/azl397985856/leetcode/blob/77db8fa47c7ee0a14b320f7c2d22f7c61ae53c35/problems/1297.maximum-number-of-occurrences-of-a-substring.md)
50+
51+
由于只有 a 和 b 两个字符。其实最多的消除次数就是 2。因为我们无论如何都可以先消除全部的 1 再消除全部的 2(先消除 2 也一样),这样只需要两次即可完成。 我们再看一下题目给的一次消除的情况,题目给的例子是“ababa”,我们发现其实它本身就是一个回文串,所以才可以一次全部消除。那么思路就有了:
52+
53+
- 如果 s 是回文,则我们需要一次消除
54+
- 否则需要两次
55+
- 一定要注意特殊情况, 对于空字符串,我们需要 0 次
56+
57+
## 代码
58+
59+
代码支持:Python3
60+
61+
Python3 Code:
62+
63+
```python
64+
65+
class Solution:
66+
def removePalindromeSub(self, s: str) -> int:
67+
if s == '':
68+
return 0
69+
def isPalindrome(s):
70+
l = 0
71+
r = len(s) - 1
72+
while l < r:
73+
if s[l] != s[r]:
74+
return False
75+
l += 1
76+
r -= 1
77+
return True
78+
return 1 if isPalindrome(s) else 2
79+
```
80+
81+
如果你觉得判断回文不是本题重点,也可以简单实现:
82+
83+
Python3 Code:
84+
85+
```python
86+
class Solution:
87+
def removePalindromeSub(self, s: str) -> int:
88+
if s == '':
89+
return 0
90+
return 1 if s == s[::-1] else 2
91+
92+
```
93+
94+
## 关键点解析
95+
96+
- 注意审题目,一定要利用题目条件“只含有 a 和 b 两个字符”否则容易做的很麻烦

0 commit comments

Comments
 (0)