Leetcode One Question of the Day 2021/01/08

[One question per day] Leetcode: 680 verification palindrome string Ⅱ

  • Given a non-empty string s, delete at most one character. Determine whether it can be a palindrome string.
  • Example 1:
输入: "aba"
输出: True
  • Example 2:
输入: "abca"
输出: True
解释: 你可以删除c字符。
  • Note: The string only contains lowercase letters from az. The maximum length of the string is 50000.

Ideas:
1
2
3

As a result, the following test cases did not pass the
3
own code:

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

Time complexity: O(n)
Space complexity: O(1)
Problem classification: Double pointer + recursion
Error point: Special circumstances are not taken into consideration

Guess you like

Origin blog.csdn.net/weixin_40910614/article/details/112384369