Problem Statement

Remove Consecutive Characters | Practice | GeeksforGeeks Remove adjacent duplicates recursively

Pattern:


Solution

public char add (StringBuilder sb, String s, int i) {
	if(i == s.length()) return '#';
	
	char next = add(sb, s, i+1);
	char curr = s.charAt(i);
	if (curr != next) sb.append(curr);
	
	return curr;
}
 
public String removeConsecutiveCharacter(String S){
	StringBuilder sb = new StringBuilder();
	add(sb, S, 0);
	return sb.reverse().toString();
} 

TC : SC :