-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_38.java
More file actions
27 lines (24 loc) · 1.02 KB
/
Problem_38.java
File metadata and controls
27 lines (24 loc) · 1.02 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
package strings;
// Recursively remove all adjacent duplicates
public class Problem_38 {
public static String removeDuplicates(String str) {
if (str == null || str.length() <= 1) {
return str; // Empty or single-character string has no duplicates to remove
}
// Check if the first two characters are duplicates
if (str.charAt(0) == str.charAt(1)) {
// Remove the first character and recursively call the function on the remaining
// string
return removeDuplicates(str.substring(1));
} else {
// If first two characters are different, prepend the first character to the
// result of the recursive call on the remaining string
return str.charAt(0) + removeDuplicates(str.substring(1));
}
}
public static void main(String[] args) {
String str = "AABBCCCD";
String result = removeDuplicates(str);
System.out.println("String after removing adjacent duplicates: " + result);
}
}