I need to find the index of the first distinct character between two strings using a recursive method.
Examples with expected outputs:
rFirstDistinctPlace("Little parcels", "Little pretzels") -> 8
rFirstDistinctPlace("Gold shadow", "gold shadow") -> 0
rFirstDistinctPlace("gold", "golda") -> 4
rFirstDistinctPlace("gold","gold") -> -1
Note: I can't use the .equals() function
The thing I'm struggling with is that I need to return -1 if the strings are equal, otherwise it works fine.
Here's my code:
public static int rFirstDistinctPlace (String s1, String s2) {
if (smallestString(s1,s2).length()==0){
return 0;
}
if(s1.charAt(0)!=s2.charAt(0))
return rFirstDistinctPlace(s1.substring(0,0),s2.substring(0,0));
return 1+rFirstDistinctPlace(s1.substring(1),s2.substring(1));
}
This is the helper method smallestString:
public static String smallestString (String s1, String s2){
if(s1.length()>s2.length()){
return s2;
}
else if (s2.length()>s1.length()){
return s1;
}
else
return s1;
}
Thank you!