Determine whether a string is a palindrome after ignoring characters that are not letters or digits and treating uppercase and lowercase letters as equal. The input contains only printable ASCII characters.
A palindrome reads the same from left to right and from right to left. In this problem, spaces, punctuation, and letter case do not affect the answer.
Instead of building a cleaned copy of the string, use two pointers to inspect it from both ends. left searches for the next alphanumeric character from the front, while right searches from the back. If either pointer reaches a character that should be ignored, move only that pointer. The ignored character has no effect on the palindrome check, while the character at the other pointer may still need to be compared.
After ignored characters have been skipped, compare the characters at the two pointers after converting them to the same case. While the pointers are still apart, they identify the next letters or digits from opposite ends. If they meet during skipping, the remaining position cannot create a mismatch. A mismatch proves the string is not a palindrome because these are the next meaningful characters from opposite ends. If they match, that pair is finished, so move both pointers inward. The search continues while left < right, and meeting or crossing means every required pair has matched.
Valid Palindrome on "Race a Car"
Each skip loop checks left < right and stops when the pointers meet. This keeps both indices inside the remaining range, including when the string contains only ignored characters.
Each pointer moves only inward and advances past each position at most once. The total time is O(n). The algorithm does not construct a cleaned string, so the two indices use O(1) auxiliary space.