Java Reference
In-Depth Information
Maybe you cut the original input in half and have solutions for each half. Then you
may need to add both solutions to arrive at a solution for the whole.
Consider the methods for simplifying the inputs for the palindrome test. Cutting
the string in half doesn't seem a good idea. If you cut
"Madam, I'm Adam"
in half, you get two strings:
"Madam, I"
and
"'m Adam"
Neither of them is a palindrome. Cutting the input in half and testing whether the
halves are palindromes seems a dead end.
The most promising simplification is to remove the first and last characters.
Removing the M at the front and the m at the back yields
"adam, I'm Ada"
Suppose you can verify that the shorter string is a palindrome. Then of course the
original string is a palindromeȌwe put the same letter in the front and the back.
That's extremely promising. A word is a palindrome if
ȗ
The first and last letters match (ignoring letter case)
and
ȗ
The word obtained by removing the first and last letters is a palindrome.
Again, don't worry how the test works for the shorter string. It just works.
There is one other case to consider. What if the first or last letter of the word is not
a letter? For example, the string
"A man, a plan, a canal, Panama!"
598
599
ends in a ! character, which does not match the A in the front. But we should
ignore non-letters when testing for palindromes. Thus, when the last character is
Search WWH ::




Custom Search