Palindrome Remove one Character

206 Views Asked by At

We are given a string $S$. If we remove just one character from the string $S$, we can get a palindrome.

Suppose $S$ $=$ $aBa$, where $B$ is the middle string, then I need to prove that I can remove one character from the string B and get a palindrome.

How do I prove this?

1

There are 1 best solutions below

0
On BEST ANSWER

Looks like a proof by contradiction. Suppose $S=aBa$ and that it is not possible to remove a character from $B$ and turn $S$ into a palindrome. Then since $S$ can be turned into a palindrome by removal of one character, that one character must be either the first or last $a$.

Suppose it's the first $a$. Then $Ba$ must be a palindrome, which means that $B$ must begin with $a$. But then I could have removed that $a$ from $B$ to obtain a palindrome, which contradicts my assumptions. So it must be the last $a$ that I can remove... but then $B$ must end in $a$ which means I could have removed that $a$ from the $B$ in the first place instead.

So the assumption that it wasn't possible to remove a character from $B$ to turn $S$ into a palindrome was wrong, and I'm done.