Watch the two-pointer technique check if a string is a palindrome step by step.
About this algorithm
A palindrome reads the same forwards and backwards once spaces and punctuation are ignored and capitals count as small letters, like "Race car!". To check a string, compare its first and last letters, then the second and second to last, and keep moving inward, stepping over anything that is not a letter or digit. One mismatch means it is not a palindrome; reaching the middle means it is.
Use this whenever you need to test a string or array for mirror symmetry without making a reversed copy. It is also the inner step of harder questions such as "valid palindrome after deleting at most one character" and expand-around-centre searches for the longest palindrome. Compared with the pair sum page, the pointers here always move together, one step each, because there is no sum to decide which side to move. Reversing the string and comparing it with the original gives the same answer but needs a second copy of the string.
left = 0 and right = length - 1.
While left < right, if s[left] is not a letter or digit, move left one step right and look again. Do the same for s[right], moving it one step left. Spaces and punctuation are never compared.
Compare s[left] with s[right] ignoring case, so R and r match. If they differ, return false. If they match, move left one step right and right one step left.
When the pointers meet or cross, every mirrored pair has matched, so return true. In an odd length word the middle letter is never compared, because it is its own mirror.
The page loads "Race car!", 9 characters, with left = 0 and right = 8. Right starts on the !, which is skipped, so right = 7. Compare R and r: a match once case is ignored, move to 1 and 6. Compare a and a, move to 2 and 5. Compare c and c, move to 3 and 4. Right is on the space, which is skipped, so right = 3 and the pointers meet on the e. The answer is true after 3 comparisons and 2 skips. Try the "Step on no cats" preset for the other outcome: S and s match, then t and t, the spaces are skipped, and then e against a fails.
This is the LeetCode version: digits count as well as letters, and case is ignored. Building a cleaned copy first (lower case, letters and digits only) and comparing that gives the same answer and is simpler to write, but the copy costs O(n) extra space. Skipping inside the loop, as this page does, keeps the extra space at O(1).
Time: each comparison retires two characters and each skip retires one, so the loop runs at most n times. Space: the check itself needs only two indices; cleaning the text into a new string first would add O(n).