Search⌘ K
AI Features

Remove All Adjacent Duplicates In String

Explore how to use stack data structures to remove adjacent duplicate letters from a lowercase string by repeatedly eliminating pairs. This lesson helps you understand the problem constraints and apply a stack-based approach to efficiently solve string manipulation tasks.

Statement

You are given a string consisting of lowercase English letters. Repeatedly remove adjacent duplicate letters, one pair at a time. Both members of a pair of adjacent duplicate letters need to be removed.

Constraints:

  • 11 \leq string.length 103\leq 10^3
  • string consists of lowercase English alphabets.

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Remove All Adjacent Duplicates In String

1.

What will be the output after removing all adjacent duplicates from the string given below?

string = “azxxzy”

A.

azzy

B.

axxy

C.

ay

D.

" "


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in the following coding playground.

Python
usercode > main.py
def remove_duplicates(s):
# Replace this placeholder return statement with your code
return ""
Remove All Adjacent Duplicates In String