Valid Palindrome

You have a palindrome checker. It works. Reverse and compare, done. s.replace(/[^a-z0-9]/gi, "").toLowerCase() strips the junk, you reverse the result, and if the two strings match, it is a palindrome. Clean, correct, one line.

Now try this: someone hands you an almost-palindrome and asks which single character you would delete to fix it. That is Palindrome II (LC 680). Your clean reversed string sits there, mute. It cannot answer the question because it forgot where its characters came from.

Let us see what that actually looks like.

The Hidden Cost

Here is an almost-palindrome: "A man, a plan, a caal: Panama" -- one letter off from the classic. The cleaned version has a mismatch at cleaned index 8. Palindrome II (LC 680) would ask: “delete one character to fix it.” You need to find and remove this character from the ORIGINAL string.

But the cleaning step stripped out nine non-alphanumeric characters -- spaces, commas, a colon -- before that index. The cleaned string lives in a different coordinate system. Where is cleaned index 8 in the original?

Cleaned copy
012345678910111213141516171819

The cleaned string shows a mismatch at cleaned index 8. You need to delete this character from the ORIGINAL string. What problem do you face?