給定一個字符串,驗證它是否是迴文串,只考慮字母和數字字符,可以忽略字母的大小寫。
說明:本題中,我們將空字符串定義爲有效的迴文串。
示例 1:
輸入: "A man, a plan, a canal: Panama"
輸出: true
示例 2:
輸入: "race a car"
輸出: false
class Solution:
def isPalindrome(self, s: str) -> bool:
import re
s = re.sub(r'[^a-z0-9]', '', s.lower())
return s == s[::-1]