[LC] 125. Valid Palindrome

时间:2019-10-21 10:06:52   收藏:0   阅读:85

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

 

原文:https://www.cnblogs.com/xuanlu/p/11711310.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!