题目
题解
思路:因为只允许最多删除一个字母,所以要么删左边,要么删右边,并且删完后只需要判断中间的还没判断的区间就可以;
class Solution:
def validPalindrome(self, s: str) -> bool:
def checkPalindrome(low, high): #判断是否是回文字符串
i, j = low, high
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
low, high = 0, len(s)-1
while low < high:
if s[low] == s[high]:
low += 1
high -= 1
else:
return checkPalindrome(low + 1, high) or checkPalindrome(low, high - 1)
return True