标签:als pen bool [] cas char rpo sid ons
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:
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s)==0:
return True
str = []
for i in s:
if i.isalnum():
str.append(i.lower())
for i in range(int(len(str)/2)):
if str[i]!= str[len(str)-i-1]:
return False
return True
标签:als pen bool [] cas char rpo sid ons
原文地址:https://www.cnblogs.com/bernieloveslife/p/9733355.html