Approach

  • there is only one pair of i, j such that s.charAt(i) != s.charAt(j)
    after the different pair hit, we try removing i or j, the characters in middle should be a palindrome.
  • there is no pair of i, j such that s.charAt(i) != s.charAt(j)

Code

class Solution:
    def validPalindrome(self, s: str) -> bool:
        left, right = 0, len(s) - 1
 
        while left < right:
            if s[left] != s[right]:
                skipL, skipR = s[left + 1:right + 1], s[left:right]
                return skipL == skipL[::-1] or skipR == skipR[::-1]
            left, right = left + 1, right - 1
        return True