Advertisement

Remove All Adjacent Duplicates in String II - LeetCode 1209 Solution

Remove All Adjacent Duplicates in String II - Complete Solution Guide

Remove All Adjacent Duplicates in String II is LeetCode problem 1209, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a string s and an integer k , a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them, causing the left and the right side of the deleted substring to concatenate together. We repeatedly make k duplicate removals on s until we no longer can. Return the final string after all such duplicate removals have been made . It is guaranteed that the answer is unique . Example 1: Input: s = "abcd", k = 2 Output: "abcd" Explanation: There's nothing to

Detailed Explanation

The problem requires you to repeatedly remove all adjacent duplicate characters of length `k` from a given string `s` until no more removals are possible. The goal is to return the final string after all such removals. For instance, if `s = "deeedbbcccbdaa"` and `k = 3`, you would first remove "eee" and "ccc" resulting in "ddbbbdaa", then remove "bbb" to get "dddaa", and finally remove "ddd" which results in the final string "aa".

Solution Approach

The solution uses a stack to keep track of the characters and their respective counts. It iterates through the input string `s`. For each character, it checks if the stack is empty or if the current character is different from the character at the top of the stack. If either is true, it adds a new pair (character, 1) to the stack. If the current character is the same as the character at the top of the stack, it increments the count of that character. After each character is processed, it checks if the count of the character at the top of the stack has reached `k`. If it has, the character-count pair is removed from the stack. Finally, the characters remaining in the stack are concatenated to form the final string.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack to store character-count pairs.
  2. Step 2: Iterate through the input string `s` character by character.
  3. Step 3: For each character, check if the stack is empty or if the current character is different from the character at the top of the stack.
  4. Step 4: If the stack is empty or the characters are different, push a new character-count pair (character, 1) onto the stack.
  5. Step 5: If the characters are the same, increment the count of the character at the top of the stack.
  6. Step 6: After each character is processed, check if the count of the character at the top of the stack is equal to `k`.
  7. Step 7: If the count is equal to `k`, pop the character-count pair from the stack.
  8. Step 8: After processing all characters in the input string, construct the final string by concatenating the remaining characters in the stack based on their respective counts.
  9. Step 9: Return the final string.

Key Insights

  • Insight 1: Using a stack is an efficient way to keep track of characters and their counts, allowing for easy removal of duplicates when the count reaches `k`.
  • Insight 2: The stack stores character-count pairs. When a new character is encountered, either the count of the last character in the stack increases (if it's the same character), or a new character-count pair is added to the stack.
  • Insight 3: Maintaining the character-count pairs lets us quickly identify and remove duplicates of length 'k' without having to re-scan the string from the beginning.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: String, Stack.

Companies

Asked at: Attentive, Disney, FactSet, Grammarly.