Maintained by Vikas Dulgunde, software engineer
You are given a string that may contain letters, digits, spaces, and punctuation. The goal is to test whether it is a palindrome, but only the alphanumeric characters count. Every space, comma, colon, underscore, or other symbol is ignored entirely.
Comparison is case insensitive, so an uppercase 'A' and a lowercase 'a' are considered the same character. After filtering and lowercasing, the remaining sequence must be identical when read left to right and right to left.
Return true when that filtered sequence is a palindrome and false otherwise. A string with no alphanumeric characters at all (including an empty string) counts as a palindrome because there is nothing that breaks the symmetry.
Stripping non-alphanumerics and lowercasing gives 'amanaplanacanalpanama', which reads the same in both directions.
The cleaned form is 'raceacar'. The first character 'r' does not match the last character 'r' once you fold inward: 'raceacar' reversed is 'racaecar', so it is not a palindrome.
A single space has no alphanumeric characters, so the filtered string is empty and is treated as a palindrome.
The underscore is skipped, leaving 'aba', which is symmetric.
Visible test cases
First picture the simplest version: build a new string of just the lowercased letters and digits, then check it against its reverse.
You do not actually need a second copy of the string. Two indices, one at each end, can walk toward the middle and compare as they go.
When a pointer lands on a non-alphanumeric character, advance that pointer without comparing. Only compare when both pointers sit on alphanumeric characters.
Reduce the messy input to a clean canonical form, then a palindrome check is trivial.
Compare from both ends inward while skipping characters that do not count, so no extra string is built.