給定一個字符串,驗證它是否是迴文串,只考慮字母和數字字符,可以忽略字母的大小寫。
說明:本題中,我們將空字符串定義爲有效的迴文串。
示例 1:
輸入: "A man, a plan, a canal: Panama"
輸出: true
示例 2:
輸入: "race a car"
輸出: false
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/valid-palindrome
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
s=re.sub('[^a-z0-9]*','',s.lower()) #注意s.lower().replace('[^a-z0-9]*','') replace不支持正則
if s!=s[::-1]:
return False
else:
return True