Java Reference
In-Depth Information
not a letter but the first character is a letter, it doesn't make sense to remove both
the first and the last characters. That's not a problem. Remove only the last
character. If the shorter string is a palindrome, then it stays a palindrome when you
attach a nonletter.
The same argument applies if the first character is not a letter. Now we have a
complete set of cases.
ȗ
If the first and last characters are both letters, then check whether they
match. If so, remove both and test the shorter string.
ȗ
Otherwise, if the last character isn't a letter, remove it and test the shorter
string.
ȗ
Otherwise, the first character isn't a letter. Remove it and test the shorter
string.
In all three cases, you can use the solution to the simpler problem to arrive at a
solution to your problem.
Step 3 Find solutions to the simplest inputs.
A recursive computation keeps simplifying its inputs. Eventually it arrives at very
simple inputs. To make sure that the recursion comes to a stop, you must deal with
the simplest inputs separately. Come up with special solutions for them, which is
usually very easy.
However, sometimes you get into philosophical questions dealing with degenerate
inputs: empty strings, shapes with no area, and so on. Then you may want to
investigate a slightly larger input that gets reduced to such a trivial input and see
what value you should attach to the degenerate inputs so that the simpler value,
when used according to the rules you discovered in Step 2, yields the correct
answer.
Let's look at the simplest strings for the palindrome test:
ȗ
Strings with two characters
ȗ
Strings with a single character
Search WWH ::




Custom Search