We determine whether a string is a valid palindrome using a two-pointer technique that compares characters from both ends while ignoring non-alphanumeric symbols and case differences.
-
Core intuition:
A palindrome mirrors itself β the first alphanumeric character must match the last alphanumeric character, the second alphanumeric must match the second-to-last alphanumeric, and so on. Because this relationship is symmetric around the center, itβs natural to start from both ends and move inward after each successful match. Each inward step confirms that one mirrored pair satisfies the palindrome property.
If at any point the two compared characters donβt match (when compared case-insensitively), the symmetry is broken and we can stop immediately β the string is not a palindrome. Otherwise, the pointers keep moving inward until they cross, which happens naturally after the final pair matches, confirming that all mirrored pairs were valid.
Note that if there is an odd number of alphanumeric characters, the pointers will meet at the middle. This meeting still results in a successful check whereleft == rightat the center, and in the next iteration, they will naturally cross each other β marking the completion of the validation. -
Pointer setup:
Two pointers are used:leftstarts at index 0 (the beginning of the string).rightstarts at the last index (the end of the string).
-
Skipping non-alphanumeric characters:
Since only letters and digits matter for palindrome checking, any non-alphanumeric characters are skipped dynamically. The conditionswhile (left < right && !isAlphanumeric(s[left])) left++;andwhile (left < right && !isAlphanumeric(s[right])) right--;ensure that both pointers always land on meaningful (alphanumeric) characters before any comparison is made.
Pointer safety and edge handling while skipping:
Theleft < rightpart ensures that the pointers never move past each other while skipping non-alphanumeric characters. This not only prevents out-of-bounds access but also guarantees correct behavior even if no alphanumeric characters remain. In that case, theleftpointer keeps advancing until it meets therightpointer andrightdoes not move β both now pointing to the same non-alphanumeric character at theright. Since the comparison phase does not re-check whether characters are alphanumeric, the equality check atleft === rightcounts as a valid match, moving both pointers inward. On the next iteration,left > rightbecomes true, ending the loop naturally and confirming the string as a valid palindrome. -
Character comparison:
Once both pointers are on valid characters:- If
s[left]ands[right]differ (case-insensitive), the string cannot be a palindrome and we stop immediately. - If they match, move both pointers inward (
left++,right--) to continue checking the next mirrored pair.
- If
-
Pointer crossing β natural termination:
The loop continues as long asleft < right. After validating the final matching pair (where both pointers may even point to the same position in the final check, as explained in the Core intuition section), theleftandrightpointers move past each other naturally. This crossing marks the successful completion of all comparisons β no mismatches were found, so the string is confirmed to be a valid palindrome.