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

  1. Use a stack-based approach
  2. Push if top != current
  3. Pop if top == current

Related Strings exercises

Practice all Strings exercises · Run this idea in the Java compiler