This question asks us if a palindrome is valid if it has at most one possible incorrect character. We have a generic is_palindrome function, and if we find that one character doesn’t match, we check either skipping this character on our left side recursion or our right side recursion.
class Solution:
def validPalindrome(self, s: str) -> bool:
def is_palindrome(s, i, j):
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
i = 0
j = len(s) - 1
while i < j:
if s[i] != s[j]:
return is_palindrome(s, i, j - 1) or is_palindrome(s, i + 1, j)
i += 1
j -= 1
return True