Show the call stack for isPalindrome("abcba") using the method defined in Listing 1.
LISTING 1 RecursivePalindrome.java
1 public class RecursivePalindrome {
2 public static boolean isPalindrome(String s) {
3 return isPalindrome(s, 0, s.length() - 1);
4 }
5
6 private static boolean isPalindrome(String s, int low, int high) {
7 if (high <= low) // Base case
8 return true;
9 else if (s.charAt(low) != s.charAt(high)) // Base case
10 return false;
11 else
12 return isPalindrome(s, low + 1, high - 1);
13 }
14
15 public static void main(String[] args) {
16 System.out.println("Is moon a palindrome? "
17 + isPalindrome("moon"));
18 System.out.println("Is noon a palindrome? "
19 + isPalindrome("noon"));
20 System.out.println("Is a a palindrome? " + isPalindrome("a"));
21 System.out.println("Is aba a palindrome? " + isPalindrome("aba"));
22 System.out.println("Is ab a palindrome? " + isPalindrome("ab"));
23 }
24 }
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.