EasyTwo Pointers
Is Palindrome II
Check palindrome with alphanumeric only
Solution Approach
Two pointers from both ends. Skip non-alphanumeric characters and compare in lowercase.
Implementation
def isPalindrome(s):
left, right = 0, len(s) - 1
while left < right:
while left < right and not s[left].isalnum():
left += 1
while left < right and not s[right].isalnum():
right -= 1
if s[left].lower() != s[right].lower():
return False
left += 1
right -= 1
return TrueComplexity Analysis
Time Complexity
O(n)Space Complexity
O(1)Complexity
Time:O(n)
Space:O(1)
Asked at
FacebookApple