|
Take a look at the recursive call to isPalindrome( ):
public static boolean isPalindrome(String w)
{
if ( w.length() <= 1 )
{ // base case
return true;
}
else
{
int lastPos = w.length() - 1;
return (w.charAt(0) == w.charAt(lastPos)) &&
isPalindrome( w.substring(1, lastPos) );
^^^^^^^^^^^^^^^^^^^^^^^
}
}
|
w.substring(1, lastPos) will create a new String (and increase usage of computer memory !!)
|
|
|
Let's start by writing the code to handle the base cases:
public static boolean isPalindrome(String w, int startPos, int endPos)
{
if ( )
{ // Base case
return true;
}
else
{
if (w.charAt(startPos) != w.charAt(endPos))
return false;
boolean ans = isPalindrome(w, startPos+1, endPos-1);
return ans;
}
}
|
Check for base cases (= strings with 0 or 1 character):
public static boolean isPalindrome(String w, int startPos, int endPos)
{
if ( startPos >= endPos )
{ // Base case
return true;
}
else
{
if (w.charAt(startPos) != w.charAt(endPos))
return false;
boolean ans = isPalindrome(w, startPos+1, endPos-1);
return ans;
}
}
|
Next we solve a smaller problem that will help us solve the original problem.
Solve a smaller problem that will help us solve the original problem:
public static boolean isPalindrome(String w, int startPos, int endPos)
{
if ( startPos >= endPos )
{ // Base case
return true;
}
else
{
// Solve a smaller problem that helps us solve the original problem
boolean helpSol = isPalindrome(w, startPos+1, endPos-1);
}
}
|
Then solve the original problem using the helpSol
Solve the original problem using the helpSol:
public static boolean isPalindrome(String w, int startPos, int endPos)
{
if ( startPos >= endPos )
{ // Base case
return true;
}
else
{
// Solve a smaller problem that helps us solve the original problem
boolean helpSol = isPalindrome(w, startPos+1, endPos-1);
return (w.charAt(startPos) == w.charAt(endPos)) && helpSol
}
}
|
We need to adjust the way we invoke the new isPalindrome( ) method in main( )...
The new (more efficient) isPalindrome( ) method is invoked with the 0 and length()−1:
public static void main(String[] args)
{
String s = "Some string";
int lastPos = s.length()-1;
ans = isPalindrome(s, 0, lastPos); // Different way to invoke isPalindrom( )
}
public static boolean isPalindrome(String w, int startPos, int endPos)
{
if ( startPos >= endPos )
{ // Base case
return true;
}
else
{
// Solve a smaller problem that helps us solve the original problem
boolean helpSol = isPalindrome(w, startPos+1, endPos-1);
return (w.charAt(startPos) == w.charAt(endPos)) && helpSol;
}
}
|
DEMO: demo/07-recursion/02-palindrome/Palindrome3.java
We can also provide the original isPalindrome( ) to the user with the help of the new isPalinndrome( ):
public static void main(String[] args)
{
String s = "Some string";
ans = isPalindrome(s); // Original way to invoke isPalindrome( )
}
public static boolean isPalindrome(String w) // The original palindrome( ) method
{
int lastPos = w.length()-1;
return isPalindrome(w, 0, lastPos);
}
public static boolean isPalindrome(String w, int startPos, int endPos)
{
if ( startPos >= endPos )
{ // Base case
return true;
}
else
{
// Solve a smaller problem that helps us solve the original problem
boolean helpSol = isPalindrome(w, startPos+1, endPos-1);
return (w.charAt(startPos) == w.charAt(endPos)) && helpSol;
}
}
|
DEMO: demo/07-recursion/02-palindrome/Palindrome4.java