Valid Palindrome
Given a string s
, return true
if it is a palindrome, otherwise return false
.
Two pointers type question.
Relies on alphaNum
check:
def alphanum(c: str) -> bool:
return (ord('A') <= ord(c) <= ord('Z') or
ord('a') <= ord(c) <= ord('z') or
ord('0') <= ord(c) <= ord('9'))
def isPalindrome(s: str) -> bool:
l, r = 0, len(s) - 1
while l < r:
while l < r and not alphanum(s[l]):
l += 1
while l < r and not alphanum(s[r]):
r += 1
if s[l].lower() != s[r].lpower():
return False
l, r = l + 1, r - 1
return True
Solution | Time | Space |
---|---|---|
Reverse string | ||
Two pointers |