680 Valid Palindrome II

Given a string s, return true if the s can be palindrome after deleting at most one character from it.

Example 1:

Input: s = "aba"
Output: true

Example 2:

Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.

Example 3:

Input: s = "abc"
Output: false

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution:
    def validPalindrome(self, s: str) -> bool:
        if s == s[::-1]:
            return True

        def rec(l, r) -> bool:
            if l >= r:
                return True
            if s[l] == s[r]:
                return rec(l + 1, r - 1)
            temp1 = s[l+1:r+1]
            temp2 = s[l:r]
            return (
                temp1 == temp1[::-1] or
                temp2 == temp2[::-1]
            )
        return rec(0, len(s) - 1)