Easy to Understand Python Solution


  • -1

    We can use the standard two-pointer approach that starts at the left and right of the string and move inwards. Whenever there is a mismatch, we can either exclude the character at the left or the right pointer. We then take the two remaining substrings and compare against its reversed and see if either one is a palindrome.

    By Yang Shun

    class Solution(object):
        def validPalindrome(self, s):
            """
            :type s: str
            :rtype: bool
            """
            # Time: O(n)
            # Space: O(n)
            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
    

  • 1

    @yangshun it's not elegant when three double quote marks come together.


  • 0

    @mzchen I don't understand. Do you mean the docstring?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.