125. Valid Palindrome
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s
, return true
if it is a palindrome, or false
otherwise.
'''
Careful with innner loop terminating condition
Careful with innner loop terminating condition
'''
def isPalindrome(s):
L = len(s)
left, right = 0, L - 1
while left < right:
while not s[left].isalnum() and left < right:
left += 1
while not s[right].isalnum() and right > left:
right -= 1
if s[left].lower() != s[right].lower():
return False
left += 1
right -= 1
return True
Comments
Post a Comment