跳到主要内容

Valid Palindrome II

描述

TODO

分析

代码

# Valid Palindrome II
# Time complexity: O(n)
# Space complexity: O(1)
class Solution:
def validPalindrome(self, s: str) -> bool:
i = 0
j = len(s) - 1

while i < j:
# Found a mismatched pair, try both deletions
if s[i] != s[j]:
return self.checkPalindrome(s, i, j - 1) or self.checkPalindrome(s, i + 1, j)

i += 1
j -= 1

return True

def checkPalindrome(self, s: str, i: int, j: int) -> bool:
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True

相关题目