Solution 1 - Two Pointers + One Deletion
On first mismatch, check if skipping either endpoint yields a palindrome.
On first mismatch, check if skipping either endpoint yields a palindrome.
""" 0680.1 - Valid Palindrome II - Solution 1 - Two Pointers """
class Solution:
def validPalindrome(self, s: str) -> bool:
def is_pal(i: int, j: int) -> bool:
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
i, j = 0, len(s) - 1
while i < j and s[i] == s[j]:
i += 1
j -= 1
return is_pal(i + 1, j) or is_pal(i, j - 1)