Leetcode 680. Verify palindrome string II | Python

Time:2021-5-9

680. Verify palindrome string II


Title Source: https://leetcode-cn.com/problems/valid-palindrome-ii

subject


Given a non empty string s, at most one character is deleted. Determine whether it can be a palindrome string.

Example 1:

Input: "ABA"
Output: true

Example 2:

Input: "ABCA"
Output: true
Explanation: you can delete the C character.

be careful:

  • The string contains only lowercase letters from A-Z. The maximum length of a string is 50000.

Thinking of solving problems


Idea: double pointer

It is allowed to delete one character at most. First of all, let’s talk about whether a string is a palindrome string without deleting it?

Similarly, a common method is double pointers. Define two pointers, one at the beginning and one at the end. At this time, judge whether the character corresponding to the pointer is the same, if not, it is not palindrome string; If they are the same, the pointers will move to the middle. Judge again until the pointers meet. Then you can confirm that the character is palindrome string.

Now the topic explains that you can delete a character. Here we also use the double pointer method. The initialization of double pointers is the same, one points to the beginning and the other to the end. At this time, we also judge whether the characters corresponding to the pointer are the same. If they are the same, as in the previous steps, the pointer moves to the middle and continues to judge until the pointers meet.

The difference is that if the characters corresponding to the current double pointer are different, you can consider deleting an element. There are two cases:

  • Delete the character corresponding to the left pointer, move the left pointer to the next bit, and judge again whether the string in the range from the character corresponding to the left pointer to the character corresponding to the right pointer at this time is palindrome string
  • Delete the character corresponding to the right pointer, move the right pointer to the previous bit, and judge again whether the string from the character corresponding to the left pointer to the character corresponding to the right pointer is palindrome string

According to the previous two cases, suppose that double pointers are defined as left and right. If the characters corresponding to the left and right pointers are different, the above two situations should be expressed as follows:

  • Move the left pointer to the next bit to judge whether the string between s [left + 1]… S [right] is palindrome string
  • Moving the pointer to the previous bit is to judge whether the string between s [left]… S [Right-1] is palindrome string

The specific code implementation is as follows.

code implementation


class Solution:
    def validPalindrome(self, s: str) -> bool:
        def check(left, right):
            #Judge whether it is palindrome number
            while left < right:
                if s[left] != s[right]:
                    return False
                left += 1
                right -= 1
            return True
        
        #Define pointers, one to the beginning and one to the end
        left, right = 0, len(s) - 1
        while left < right:
            #When the characters corresponding to the pointer are the same, the pointer moves to the middle
            if s[left] == s[right]:
                left += 1
                right -= 1
            #If the character corresponding to the pointer is different, consider deleting a character
            #1. Delete the character of the current left pointer and move to the next digit
            #2. Delete the character of the current right pointer and move to the previous one
            #Re judge whether the string is palindrome string after deleting characters
            else:
                return check(left + 1, right) or check(left, right - 1)
        return True

Implementation results


Leetcode 680. Verify palindrome string II | Python


summary

  • Use the double pointer method to define double pointers, one pointing to the beginning position and the other pointing to the end. Determine whether the characters corresponding to the pointer are the same. If it is the same, move the pointer to the middle and continue to judge until the pointers meet. If not, consider deleting a character, as follows.
  • Delete the character of the left pointer, move the left pointer to the next bit, and judge the string again.
  • Delete the character of the right pointer, move the right pointer to the previous bit, and judge again.
  • As long as one of the above two conditions meets, it will return true, otherwise it will return false.

Welcome to WeChat official account, book collection.

Recommended Today

Basic use of helm package manager of container orchestration system k8s (I)

Earlier, we learned about the use of HPA resources on k8s. For review, please refer to:https://www.cnblogs.com/qiuhom-1874/p/14293237.html; Today, let’s talk about k8s package manager helm; What is helm? If we compare the resource list of k8s to the RPM package on CentOS, helm works like Yum; In short, helm is a package manager like yum. It […]