[leetcode][125]Valid Palindrome

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

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

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

Subscribe to see which companies asked this question



import string
class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        valid = set(string.ascii_lowercase+ string.digits)
        
        left = 0
        right = len(s) -1
        while left < right:
            s_l = s[left].lower()
            s_r = s[right].lower()
            
            if (s_l in valid) and (s_r in valid):

                if s_l == s_r:
                    left += 1
                    right -= 1
                else:
                    return False
                    
            elif s_l in valid:
                right -= 1
            elif s_r in valid:
                left += 1
            else:
                left += 1
                right -= 1
        return True
                
        


全部评论

相关推荐

2025-12-30 16:42
同济大学 C++
仁狂躁使者:哎呀,不用担心,我当时配环境配了两天,项目捋不清就问问导师能不能用ai,慢慢就清了,会好起来的
点赞 评论 收藏
分享
小肥罗:我觉得“实习生不了解也很正常”可能只是客套话,面试官的标准是希望答上来。另外,面试官没有马上结束面试,恰恰证明他想给你机会,想多考察你看你是否其他方面符合要求。面试时间长反而证明面试官还是看好你的,想多给你机会表现一下自己。
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务