python leetcode125验证回文串

题目:

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

class Solution:
    def isPalindrome(self, s: str) -> bool:
        res = []
        if s.isspace():
            return True
        else:
            for i in s:
                if i.isalnum():
                    res.append(i)
            res = "".join(res)
            if res[:].lower() == res[::-1].lower():
                return True
            else:
                return False

s = "race a car"
S = Solution()
result = S.isPalindrome(s)
print(result)

你可能感兴趣的:(算法,python,开发语言,后端)