-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathRemoveDuplicate.java
More file actions
40 lines (36 loc) · 921 Bytes
/
RemoveDuplicate.java
File metadata and controls
40 lines (36 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package recursion2;
import java.util.Scanner;
/*Given a string S, remove consecutive duplicates from it recursively.
Input Format :
String S
Output Format :
Output string
Constraints :
1 <= |S| <= 10^3
where |S| represents the length of string
Sample Input 1 :
aabccba
Sample Output 1 :
abcba
Sample Input 2 :
xxxyyyzwwzzz
Sample Output 2 :
xyzwz*/
public class RemoveDuplicate {
public static String removeDuplicates(String str) {
if (str.length() <= 1) {
return str;
}
String answer = "";
if (str.charAt(0) != str.charAt(1)) {
answer += str.charAt(0);
}
String smallAnswer = removeDuplicates(str.substring(1));
return answer + smallAnswer;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
System.out.println(removeDuplicates(str));
}
}