-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesRecursuvely.java
More file actions
46 lines (41 loc) · 1.17 KB
/
RemoveDuplicatesRecursuvely.java
File metadata and controls
46 lines (41 loc) · 1.17 KB
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
41
42
43
44
45
46
// Remove Duplicates Recursively
// Send Feedback
// 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
import java.util.Scanner;
public class RemoveDuplicatesRecursuvely {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter String : ");
String s = sc.nextLine();
String ans = removeConsecutiveDuplicates(s);
System.out.print("Output : " + ans);
sc.close();
}
public static String removeConsecutiveDuplicates(String s) {
if (s.length() <= 1) {
return s;
}
if (s.charAt(0) == s.charAt(1)) {
String smallOutput = removeConsecutiveDuplicates(s.substring(1));
return smallOutput;
} else {
String smallOutput = removeConsecutiveDuplicates(s.substring(1));
return s.charAt(0) + smallOutput;
}
}
}