Remove All Adjacent Duplicates In String

You are given a string `s` of lowercase English letters. A duplicate removal picks two adjacent, equal letters and deletes both. Repeat duplicate removals until no more can be made, and return the final string. The answer is unique.

30:00

Remove All Adjacent Duplicates In String
easy
Free written solution
Topics
Companies

You are given a string s of lowercase English letters. A duplicate removal picks two adjacent, equal letters and deletes both. Repeat duplicate removals until no more can be made, and return the final string. The answer is unique.

Example 1:
Input: {"s":"abbaca"}
Output: ca
Constraints:
  • 1≤s.length≤1051 \leq s.\text{length} \leq 10^5

  • s consists of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"abbaca"}

s; matching colours = a pair that cancelled

a

0

b

1

b

2

a

3

c

4

a

5

Read the letters left to right. The stack holds the letters kept so far, so its top is the letter just before the next one.

stack of kept letters (bottom → top)

size 0

empty

pairs removed 0
letters read 0 / 6
Variables
VariableValue
stack
pairsRemoved0
DepthFunction Call
Stack empty
0/13