-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode_680_0041.py
52 lines (44 loc) · 1.04 KB
/
LeetCode_680_0041.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。
#
# 示例 1:
#
#
# 输入: "aba"
# 输出: True
#
#
# 示例 2:
#
#
# 输入: "abca"
# 输出: True
# 解释: 你可以删除c字符。
#
#
# 注意:
#
#
# 字符串只包含从 a-z 的小写字母。字符串的最大长度是50000。
#
# Related Topics 字符串
# leetcode submit region begin(Prohibit modification and deletion)
import string
class Solution:
def validPalindrome(self, s: str) -> bool:
l, r = 0, len(s) - 1
while l < r:
if s[l] == s[r]:
l += 1
r -= 1
else:
return self.is_Palindrome(s, l + 1, r) or self.is_Palindrome(s, l, r - 1)
return True
def is_Palindrome(self, s, i, j):
while i < j:
if s[i] == s[j]:
i, j = i + 1, j + 1
else:
return False
return True
# leetcode submit region end(Prohibit modification and deletion)
print(Solution().validPalindrome("aba"))