125. Valid Palindrome
Input:
"A man, a plan, a canal: Panama"
Output:
trueInput:
"race a car"
Output:
falseclass Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
s = s.lower()
i , j = 0, len(s) - 1
while i <= j:
while i < j and not s[i].isalnum(): # must be i < j in order to prevent index out of range. e.g ".,"
i +=1
while i < j and not s[j].isalnum(): # must be i < j in order to prevent index out of range. e.g ".,"
j -= 1
if s[i] != s[j]: return False
i+= 1; j -= 1
return TrueLast updated
Was this helpful?