So my assignment is to create a function in Java that takes out duplicate characters from the string. The catch is that it need to be done recursively (no loops) and it must start like this with one parameter where the string is passed in. Please help guys I've been working on this for hours and can't get it to work. :(
public static String removeDuplicateChars(String str){
This is what I have so far...it's probably really bad...
public static String removeDuplicateChars(String str){
int length=str.length();
//if string is 1 character long, cannot have a repeating character
if(length<=1)
return str;
char last=str.charAt(length-1);
char secondLast=str.charAt(length-2);
if last character is equal to second to last char
if(last==secondLast){
str=str.substring(0, length-2) + last;
System.out.print(str);
return removeDuplicateChars(str.substring(0, length-1));
}
return removeDuplicateChars(str.substring(0, length-1));