LeetCode 125 Valid Palindrome - Implementation

The approach

Two pointers skip non-alphanumeric characters and compare case-insensitively.

function isPalindrome(s): left = 0 right = len(s) - 1 while left < right: while left < right and not isAlphanumeric(s[left]): left += 1 while left < right and not isAlphanumeric(s[right]): right -= 1 if toLower(s[left]) != toLower(s[right]): return false left += 1 right -= 1 return true

O(n)O(n) time, O(1)O(1) space.