AlgoMaster Logo

Remove All Adjacent Duplicates In String

Ashish

Ashish Pratap Singh

easy

Problem Description

Solve it on LeetCode

Approaches

1. Using a Stack

Intuition:

The problem requires removal of adjacent duplicates in the given string s. This can be efficiently solved using a stack. The basic idea is to iterate over each character of the string, and use a stack to keep track of characters, ensuring that whenever two consecutive characters are the same, we pop them from the stack, effectively removing those duplicates.

Algorithm:

  1. Initialize an empty stack to store characters.
  2. Iterate through each character c in the string s.
  3. For each character, check if the stack is not empty and the top of the stack is equal to c.
    • If true, pop the top element (this means we've found a duplicate).
    • Else, push the current character c onto the stack.
  4. Finally, construct the resulting string from the characters left in the stack.

Code: