Skip to content

Commit e5854a1

Browse files
committed
feat: add solutions to lc problem: No.0680. Valid Palindrome II
1 parent 5fd8253 commit e5854a1

File tree

4 files changed

+112
-12
lines changed

4 files changed

+112
-12
lines changed

solution/0600-0699/0680.Valid Palindrome II/README.md

+37-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<li>字符串只包含从 a-z 的小写字母。字符串的最大长度是50000。</li>
3030
</ol>
3131

32-
3332
## 解法
3433

3534
<!-- 这里可写通用的实现逻辑 -->
@@ -41,15 +40,50 @@
4140
<!-- 这里可写当前语言的特殊实现逻辑 -->
4241

4342
```python
44-
43+
class Solution:
44+
def validPalindrome(self, s: str) -> bool:
45+
def isPalindrome(s):
46+
i, j = 0, len(s) - 1
47+
while i < j:
48+
if s[i] != s[j]:
49+
return False
50+
i += 1
51+
j -= 1
52+
return True
53+
54+
i, j = 0, len(s) - 1
55+
while i < j:
56+
if s[i] != s[j]:
57+
return isPalindrome(s[i: j]) or isPalindrome(s[i + 1: j + 1])
58+
i += 1
59+
j -= 1
60+
return True
4561
```
4662

4763
### **Java**
4864

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

5167
```java
52-
68+
class Solution {
69+
public boolean validPalindrome(String s) {
70+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
71+
if (s.charAt(i) != s.charAt(j)) {
72+
return isPalindrome(s.substring(i, j)) || isPalindrome(s.substring(i + 1, j + 1));
73+
}
74+
}
75+
return true;
76+
}
77+
78+
private boolean isPalindrome(String s) {
79+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
80+
if (s.charAt(i) != s.charAt(j)) {
81+
return false;
82+
}
83+
}
84+
return true;
85+
}
86+
}
5387
```
5488

5589
### **...**

solution/0600-0699/0680.Valid Palindrome II/README_EN.md

+38-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66

77
<p>
88

9-
Given a non-empty string <code>s</code>, you may delete <b>at most</b> one character. Judge whether you can make it a palindrome.
9+
Given a non-empty string <code>s</code>, you may delete <b>at most</b> one character. Judge whether you can make it a palindrome.
1010

1111
</p>
1212

13-
14-
1513
<p><b>Example 1:</b><br />
1614

1715
<pre>
@@ -24,8 +22,6 @@ Given a non-empty string <code>s</code>, you may delete <b>at most</b> one chara
2422

2523
</p>
2624

27-
28-
2925
<p><b>Example 2:</b><br />
3026

3127
<pre>
@@ -40,8 +36,6 @@ Given a non-empty string <code>s</code>, you may delete <b>at most</b> one chara
4036

4137
</p>
4238

43-
44-
4539
<p><b>Note:</b><br>
4640

4741
<ol>
@@ -61,13 +55,48 @@ The maximum length of the string is 50000.</li>
6155
### **Python3**
6256

6357
```python
64-
58+
class Solution:
59+
def validPalindrome(self, s: str) -> bool:
60+
def isPalindrome(s):
61+
i, j = 0, len(s) - 1
62+
while i < j:
63+
if s[i] != s[j]:
64+
return False
65+
i += 1
66+
j -= 1
67+
return True
68+
69+
i, j = 0, len(s) - 1
70+
while i < j:
71+
if s[i] != s[j]:
72+
return isPalindrome(s[i: j]) or isPalindrome(s[i + 1: j + 1])
73+
i += 1
74+
j -= 1
75+
return True
6576
```
6677

6778
### **Java**
6879

6980
```java
70-
81+
class Solution {
82+
public boolean validPalindrome(String s) {
83+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
84+
if (s.charAt(i) != s.charAt(j)) {
85+
return isPalindrome(s.substring(i, j)) || isPalindrome(s.substring(i + 1, j + 1));
86+
}
87+
}
88+
return true;
89+
}
90+
91+
private boolean isPalindrome(String s) {
92+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
93+
if (s.charAt(i) != s.charAt(j)) {
94+
return false;
95+
}
96+
}
97+
return true;
98+
}
99+
}
71100
```
72101

73102
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean validPalindrome(String s) {
3+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
4+
if (s.charAt(i) != s.charAt(j)) {
5+
return isPalindrome(s.substring(i, j)) || isPalindrome(s.substring(i + 1, j + 1));
6+
}
7+
}
8+
return true;
9+
}
10+
11+
private boolean isPalindrome(String s) {
12+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
13+
if (s.charAt(i) != s.charAt(j)) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def validPalindrome(self, s: str) -> bool:
3+
def isPalindrome(s):
4+
i, j = 0, len(s) - 1
5+
while i < j:
6+
if s[i] != s[j]:
7+
return False
8+
i += 1
9+
j -= 1
10+
return True
11+
12+
i, j = 0, len(s) - 1
13+
while i < j:
14+
if s[i] != s[j]:
15+
return isPalindrome(s[i: j]) or isPalindrome(s[i + 1: j + 1])
16+
i += 1
17+
j -= 1
18+
return True

0 commit comments

Comments
 (0)