Description: Given a string s, return true if it is a palindrome, or false otherwise.
Code:
# Using operations on the fly
def isPalindrome(self, s: str) -> bool:
left = 0
right = len(s)-1
while(left<=right):
if not s[left].isalnum():
left +=1
continue
if not s[right].isalnum():
right -=1
continue
if s[left].lower() != s[right].lower():
return False
left += 1
right -= 1
return True
Efficiency:
Time Complexity: O(n)
Space Complexity: O(1)
Test:
s = "A man, a plan, a canal: Panama"
print(isPalindrome(s)) #True
s = "race a car"
print(isPalindrome(s)) #False
s = " "
print(isPalindrome(s)) #True