Skip to content

Commit 999df59

Browse files
committed
feat: add solutions to lc problem: No.1332
No.1332.Remove Palindromic Subsequences
1 parent 7ddafcc commit 999df59

File tree

7 files changed

+86
-117
lines changed

7 files changed

+86
-117
lines changed

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

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
- 如果字符串 s 本身是个回文串,那么只需要删除 1 次。
59-
- 如果字符串 s 不是个回文串,我们注意到 s 最多只有两种字母 "a", "b",并且删除的是一个子序列,因此可以先删除所有字母 "a" (`"aaa...aaa"` 是个回文串),再删除所有字母 "b",即可使得字符串变为空。因此需要的删除次数是 2 次。
58+
**方法一:脑筋急转弯**
59+
60+
如果字符串 $s$ 本身是个回文串,那么只需要删除 $1$ 次。
61+
62+
如果字符串 $s$ 不是个回文串,那么先删除所有的 `'a'`,再删除所有的 `'b'`,总共需要删除 $2$ 次。
63+
64+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
6065

6166
<!-- tabs:start -->
6267

@@ -67,11 +72,7 @@
6772
```python
6873
class Solution:
6974
def removePalindromeSub(self, s: str) -> int:
70-
if not s:
71-
return 0
72-
if s[::-1] == s:
73-
return 1
74-
return 2
75+
return 1 if s[::-1] == s else 2
7576
```
7677

7778
### **Java**
@@ -81,40 +82,28 @@ class Solution:
8182
```java
8283
class Solution {
8384
public int removePalindromeSub(String s) {
84-
if (s.length() == 0) {
85-
return 0;
86-
}
87-
if (new StringBuilder(s).reverse().toString().equals(s)) {
88-
return 1;
85+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
86+
if (s.charAt(i) != s.charAt(j)) {
87+
return 2;
88+
}
8989
}
90-
return 2;
90+
return 1;
9191
}
9292
}
9393
```
9494

95-
### **TypeScript**
96-
97-
```ts
98-
function removePalindromeSub(s: string): number {
99-
if (s.length == 0) return 0;
100-
if (s == s.split('').reverse().join('')) return 1;
101-
return 2;
102-
}
103-
```
104-
10595
### **C++**
10696

10797
```cpp
10898
class Solution {
10999
public:
110100
int removePalindromeSub(string s) {
111-
if (s.empty())
112-
return 0;
113-
string t = s;
114-
reverse(s.begin(), s.end());
115-
if (s == t)
116-
return 1;
117-
return 2;
101+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
102+
if (s[i] != s[j]) {
103+
return 2;
104+
}
105+
}
106+
return 1;
118107
}
119108
};
120109
```
@@ -123,21 +112,25 @@ public:
123112
124113
```go
125114
func removePalindromeSub(s string) int {
126-
if len(s) == 0 {
127-
return 0
115+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
116+
if s[i] != s[j] {
117+
return 2
118+
}
128119
}
129-
if s == reverse(s) {
130-
return 1
131-
}
132-
return 2
120+
return 1
133121
}
122+
```
134123

135-
func reverse(s string) string {
136-
r := []byte(s)
137-
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
138-
r[i], r[j] = r[j], r[i]
139-
}
140-
return string(r)
124+
### **TypeScript**
125+
126+
```ts
127+
function removePalindromeSub(s: string): number {
128+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
129+
if (s[i] !== s[j]) {
130+
return 2;
131+
}
132+
}
133+
return 1;
141134
}
142135
```
143136

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

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,52 +56,36 @@ Remove palindromic subsequence &quot;baab&quot; then &quot;b&quot;.
5656
```python
5757
class Solution:
5858
def removePalindromeSub(self, s: str) -> int:
59-
if not s:
60-
return 0
61-
if s[::-1] == s:
62-
return 1
63-
return 2
59+
return 1 if s[::-1] == s else 2
6460
```
6561

6662
### **Java**
6763

6864
```java
6965
class Solution {
7066
public int removePalindromeSub(String s) {
71-
if (s.length() == 0) {
72-
return 0;
73-
}
74-
if (new StringBuilder(s).reverse().toString().equals(s)) {
75-
return 1;
67+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
68+
if (s.charAt(i) != s.charAt(j)) {
69+
return 2;
70+
}
7671
}
77-
return 2;
72+
return 1;
7873
}
7974
}
8075
```
8176

82-
### **TypeScript**
83-
84-
```ts
85-
function removePalindromeSub(s: string): number {
86-
if (s.length == 0) return 0;
87-
if (s == s.split('').reverse().join('')) return 1;
88-
return 2;
89-
}
90-
```
91-
9277
### **C++**
9378

9479
```cpp
9580
class Solution {
9681
public:
9782
int removePalindromeSub(string s) {
98-
if (s.empty())
99-
return 0;
100-
string t = s;
101-
reverse(s.begin(), s.end());
102-
if (s == t)
103-
return 1;
104-
return 2;
83+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
84+
if (s[i] != s[j]) {
85+
return 2;
86+
}
87+
}
88+
return 1;
10589
}
10690
};
10791
```
@@ -110,21 +94,25 @@ public:
11094
11195
```go
11296
func removePalindromeSub(s string) int {
113-
if len(s) == 0 {
114-
return 0
115-
}
116-
if s == reverse(s) {
117-
return 1
97+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
98+
if s[i] != s[j] {
99+
return 2
100+
}
118101
}
119-
return 2
102+
return 1
120103
}
104+
```
121105

122-
func reverse(s string) string {
123-
r := []byte(s)
124-
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
125-
r[i], r[j] = r[j], r[i]
126-
}
127-
return string(r)
106+
### **TypeScript**
107+
108+
```ts
109+
function removePalindromeSub(s: string): number {
110+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
111+
if (s[i] !== s[j]) {
112+
return 2;
113+
}
114+
}
115+
return 1;
128116
}
129117
```
130118

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Solution {
22
public:
33
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;
4+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
5+
if (s[i] != s[j]) {
6+
return 2;
7+
}
8+
}
9+
return 1;
1110
}
1211
};
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
func removePalindromeSub(s string) int {
2-
if len(s) == 0 {
3-
return 0
2+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
3+
if s[i] != s[j] {
4+
return 2
5+
}
46
}
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)
7+
return 1
178
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution {
22
public int removePalindromeSub(String s) {
3-
if (s.length() == 0) {
4-
return 0;
3+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
4+
if (s.charAt(i) != s.charAt(j)) {
5+
return 2;
6+
}
57
}
6-
if (new StringBuilder(s).reverse().toString().equals(s)) {
7-
return 1;
8-
}
9-
return 2;
8+
return 1;
109
}
1110
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
class Solution:
22
def removePalindromeSub(self, s: str) -> int:
3-
if not s:
4-
return 0
5-
if s[::-1] == s:
6-
return 1
7-
return 2
3+
return 1 if s[::-1] == s else 2
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function removePalindromeSub(s: string): number {
2-
if (s.length == 0) return 0;
3-
if (s == s.split('').reverse().join('')) return 1;
4-
return 2;
2+
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
3+
if (s[i] !== s[j]) {
4+
return 2;
5+
}
6+
}
7+
return 1;
58
}

0 commit comments

Comments
 (0)