Skip to content

Commit 450ce41

Browse files
committed
feat: add solutions to lc problem: No.1332. Remove Palindromic
Subsequences
1 parent f1588be commit 450ce41

File tree

6 files changed

+163
-3
lines changed

6 files changed

+163
-3
lines changed

solution/1300-1399/1332.Remove Palindromic Subsequences/README.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,85 @@
5454
<li><code>s</code> 仅包含字母&nbsp;&#39;a&#39;&nbsp; 和 &#39;b&#39;</li>
5555
</ul>
5656

57-
5857
## 解法
5958

6059
<!-- 这里可写通用的实现逻辑 -->
6160

61+
- 如果字符串 s 是个空串,直接返回 0。
62+
- 如果字符串 s 本身是个回文串,那么只需要删除 1 次。
63+
- 如果字符串 s 不是个回文串,我们注意到 s 最多只有两种字母 "a", "b",并且删除的是一个子序列,因此可以先删除所有字母 "a" (`"aaa...aaa"` 是个回文串),再删除所有字母 "b",即可使得字符串变为空。因此需要的删除次数是 2 次。
64+
6265
<!-- tabs:start -->
6366

6467
### **Python3**
6568

6669
<!-- 这里可写当前语言的特殊实现逻辑 -->
6770

6871
```python
69-
72+
class Solution:
73+
def removePalindromeSub(self, s: str) -> int:
74+
if not s:
75+
return 0
76+
if s[::-1] == s:
77+
return 1
78+
return 2
7079
```
7180

7281
### **Java**
7382

7483
<!-- 这里可写当前语言的特殊实现逻辑 -->
7584

7685
```java
86+
class Solution {
87+
public int removePalindromeSub(String s) {
88+
if (s.length() == 0) {
89+
return 0;
90+
}
91+
if (new StringBuilder(s).reverse().toString().equals(s)) {
92+
return 1;
93+
}
94+
return 2;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int removePalindromeSub(string s) {
105+
if (s.empty())
106+
return 0;
107+
string t = s;
108+
reverse(s.begin(), s.end());
109+
if (s == t)
110+
return 1;
111+
return 2;
112+
}
113+
};
114+
```
77115
116+
### **Go**
117+
118+
```go
119+
func removePalindromeSub(s string) int {
120+
if len(s) == 0 {
121+
return 0
122+
}
123+
if s == reverse(s) {
124+
return 1
125+
}
126+
return 2
127+
}
128+
129+
func reverse(s string) string {
130+
r := []byte(s)
131+
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
132+
r[i], r[j] = r[j], r[i]
133+
}
134+
return string(r)
135+
}
78136
```
79137

80138
### **...**

solution/1300-1399/1332.Remove Palindromic Subsequences/README_EN.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,68 @@ Remove palindromic subsequence &quot;baab&quot; then &quot;b&quot;.
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def removePalindromeSub(self, s: str) -> int:
60+
if not s:
61+
return 0
62+
if s[::-1] == s:
63+
return 1
64+
return 2
5965
```
6066

6167
### **Java**
6268

6369
```java
70+
class Solution {
71+
public int removePalindromeSub(String s) {
72+
if (s.length() == 0) {
73+
return 0;
74+
}
75+
if (new StringBuilder(s).reverse().toString().equals(s)) {
76+
return 1;
77+
}
78+
return 2;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int removePalindromeSub(string s) {
89+
if (s.empty())
90+
return 0;
91+
string t = s;
92+
reverse(s.begin(), s.end());
93+
if (s == t)
94+
return 1;
95+
return 2;
96+
}
97+
};
98+
```
6499
100+
### **Go**
101+
102+
```go
103+
func removePalindromeSub(s string) int {
104+
if len(s) == 0 {
105+
return 0
106+
}
107+
if s == reverse(s) {
108+
return 1
109+
}
110+
return 2
111+
}
112+
113+
func reverse(s string) string {
114+
r := []byte(s)
115+
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
116+
r[i], r[j] = r[j], r[i]
117+
}
118+
return string(r)
119+
}
65120
```
66121

67122
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int removePalindromeSub(string s) {
4+
if (s.empty())
5+
return 0;
6+
string t = s;
7+
reverse(s.begin(), s.end());
8+
if (s == t)
9+
return 1;
10+
return 2;
11+
}
12+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func removePalindromeSub(s string) int {
2+
if len(s) == 0 {
3+
return 0
4+
}
5+
if s == reverse(s) {
6+
return 1
7+
}
8+
return 2
9+
}
10+
11+
func reverse(s string) string {
12+
r := []byte(s)
13+
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
14+
r[i], r[j] = r[j], r[i]
15+
}
16+
return string(r)
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int removePalindromeSub(String s) {
3+
if (s.length() == 0) {
4+
return 0;
5+
}
6+
if (new StringBuilder(s).reverse().toString().equals(s)) {
7+
return 1;
8+
}
9+
return 2;
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def removePalindromeSub(self, s: str) -> int:
3+
if not s:
4+
return 0
5+
if s[::-1] == s:
6+
return 1
7+
return 2

0 commit comments

Comments
 (0)