LeetCode 1209: Remove All Adjacent Duplicates in String II Solution

Master LeetCode problem 1209 (Remove All Adjacent Duplicates in String II), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1209. Remove All Adjacent Duplicates in String II

Problem Explanation

Explanation

To solve this problem, we can use a stack to keep track of characters and their counts. We iterate through the input string and for each character, we check if it is the same as the character at the top of the stack. If it is, we increment the count. If the count becomes equal to k, we remove the characters from the stack. Otherwise, we push the character and its count onto the stack. At the end, we construct the final string by appending the characters left in the stack.

Algorithm:

  1. Initialize an empty stack to store pairs of character and count.
  2. Iterate through each character in the input string.
  3. For each character:
    • If the stack is not empty and the current character is the same as the character at the top of the stack, increment the count.
    • If the count becomes equal to k, remove the characters from the stack.
    • Otherwise, push the character and its count onto the stack.
  4. Construct the final string by appending the characters left in the stack.

Time Complexity: O(n) where n is the length of the input string s. Space Complexity: O(n) where n is the length of the input string s.

Solution Code

class Solution {
    public String removeDuplicates(String s, int k) {
        Stack<Pair<Character, Integer>> stack = new Stack<>();
        
        for (char ch : s.toCharArray()) {
            if (!stack.isEmpty() && stack.peek().getKey() == ch) {
                int count = stack.peek().getValue() + 1;
                if (count == k) {
                    stack.pop();
                } else {
                    stack.push(new Pair<>(ch, count));
                }
            } else {
                stack.push(new Pair<>(ch, 1));
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for (Pair<Character, Integer> pair : stack) {
            for (int i = 0; i < pair.getValue(); i++) {
                sb.append(pair.getKey());
            }
        }
        
        return sb.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1209 (Remove All Adjacent Duplicates in String II)?

This page provides optimized solutions for LeetCode problem 1209 (Remove All Adjacent Duplicates in String II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1209 (Remove All Adjacent Duplicates in String II)?

The time complexity for LeetCode 1209 (Remove All Adjacent Duplicates in String II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1209 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1209 in Java, C++, or Python.

Back to LeetCode Solutions