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));

Any help would be appreciated...I'm starting to freak out that I won't be able to figure this out by tomorrow :/

I wrote something that works fairly. Linear time Does not handle if the last character is a dupe. That is left for you to fix.

public static void main(String [] args) {
		System.out.println( dupe("assdeegfddaa") );
	}
	
	public static String dupe(String str) {
		return dupeHelper( str, "", "" );
	}
	
	public static String dupeHelper( String str, String last, String ns ) {
		
		if ( str.length() <= 1 ) {
			return ns+str;
		}
		
		String current = str.substring(0,1);
		
		if( current.equals(last) ) {
			return ns + dupeHelper( str.substring(1), current, ns );
		}
		
		else {
			return ns + current + dupeHelper( str.substring(1), current, ns );
		}
		
	}

What do you mean by linear time?

I mean that it takes time proportional to the amount of characters in str, which is as fast as it can be.

the last character dupe can be handled in the <= 1 case.

if ( str.length() <= 1 ) {
			if ( str.equals(last) ) return ns;
			return ns + str;
		}

I've helped you a little to much now. Make sure you understand the code. It's pretty simple.

public class Main {
	

	public static void main(String [] args) {
		System.out.println( duper("aaaaasssdddddfdeggghhh") );
	}
	
	public static String duper( String str ) {
		
		if ( str.length() <= 1 ) {
			return str;
		}
		
		String current = str.substring(0,1);
		String next = str.substring(1,2);
		String rest = str.substring(1);
		
		if( next.equals(current) ) {
			return duper(rest);	
		}
		else {
			return current + duper(rest);
		}
		
	}
	
}

I'm sorry normally I never ask for help or questions its just some concepts it takes me a while to understand.

Thank you so much for your help and I will definitely make sure I understand the code.

Yea i know recursion can be a bitch when your'e not used to it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.