[LC] 125. Valid Palindrome

Posted xuanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LC] 125. Valid Palindrome相关的知识,希望对你有一定的参考价值。

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if s is ‘‘:
            return True
        import string
        valid = set(string.ascii_letters + string.digits) 
        s_cp = s[:].lower()
        left, right = 0, len(s) - 1
        while left <= right:
            if s_cp[left] not in valid:
                left += 1
            elif s_cp[right] not in valid:
                right -= 1
            elif s_cp[left] != s_cp[right]:
                return False
            else:
                left += 1
                right -= 1
        return True

 

以上是关于[LC] 125. Valid Palindrome的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 1216. Valid Palindrome III

LC 593. Valid Square

[LC] 367. Valid Perfect Square

125. Valid Palindrome

125. Valid Palindrome

125. Valid Palindrome