X272: Recursion Programming Exercise: Is Reverse

For function isReverse, write the two missing base case conditions. Given two strings, this function returns true if the two strings are identical, but are in reverse order. Otherwise it returns false. For example, if the inputs are "tac" and "cat", then the function should return true.

Examples:

isReverse("tac", "cat") -> true

Your Answer:

x
 
1
public boolean isReverse(String s1, String s2) {
2
  if <<Missing condition 1>>
3
    return true;
4
  else if <<Missing condition 2>>
5
    return false;
6
  else {
7
    String s1first = s1.substring(0, 1);
8
    String s2last = s2.substring(s2.length() - 1);
9
    return s1first.equals(s2last) &&
10
           isReverse(s1.substring(1), s2.substring(0, s2.length() - 1));
11
  }
12
}
13

Feedback

Your feedback will appear here when you check your answer.