马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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
Constraints:
s consists only of printable ASCII characters.
class Solution:
def isPalindrome(self, s: str) -> bool:
copy = s[:]
s = list(s.lower())
start = 0
end = len(s) - 1
while start < end:
while start < end and not (97 <= ord(s[start]) <= 122 or 48 <= ord(s[start]) <= 57):
start += 1
while start < end and not (97 <= ord(s[end]) <= 122 or 48 <= ord(s[end]) <= 57):
end -= 1
if s[start] == s[end]:
start += 1
end -= 1
else:
return False
return True
|