aboutsummaryrefslogtreecommitdiff
path: root/valid_palindrome2.py
blob: 35d72984ee287e1ad50ac64de08a0575329ac466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 680. Valid Palindrome II

"""
given a string 's', return 'true' if the 's' can be palindrome after deleting
at most one charater from it.
"""


class Solution(object):
    def validPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        left, right = 0, len(s) - 1
        while left < right:
            if s[left] != s[right]:
                one, two = s[left:right], s[left + 1 : right + 1]
                return one == one[::-1] or two == two[::-1]
            left, right = left + 1, right - 1
        return True


if __name__ == "__main__":
    obj = Solution()
    print(obj.validPalindrome("aba"))  # expect: True
    print(obj.validPalindrome("abca"))  # expect: True
    print(obj.validPalindrome("abc"))  # expect: False