Remove Adjacent Duplicates in Java: Explanation & Practice
Remove adjacent duplicate characters
Problem summary
Remove all adjacent duplicate characters repeatedly until no more can be removed.
Starter code
public class Main {
public static void main(String[] args) {
String str = "abbaca"; // Test case 1
// Remove adjacent duplicates
// Print: <result>
}
}Expected output and test cases
- abbaca → aaca → ca
ca
- No adjacent duplicates
abc
- All removed: aabbcc
Hints
- Use a stack-based approach
- Push if top != current
- Pop if top == current
Related Strings exercises
- Practice String Compression in Java
- Practice Alphabetical Character Frequency in Java
- Practice Zigzag Conversion in Java
Practice all Strings exercises · Run this idea in the Java compiler