Skills101
← All coding problems

Remove All Adjacent Duplicates

easyLinked List & Stack II+10 XPTime limit 2s

A word game: whenever two neighbouring letters are equal, both of them vanish, and the letters around them close the gap — which may create a new equal pair. Keep going until no equal neighbours are left. Print the final string.

Example: abbaca → remove bb → aaca → remove aa → ca.

Input: one line, a string of lowercase letters.

Output: the final string, or empty if every letter vanished.

Constraints: 1 ≤ length ≤ 2·10⁵.

Think about it: the order of removals does not change the final answer. Scan left to right with a stack: if the new letter equals the top, pop; otherwise push. The stack holds the answer.

Sample input 1

abbaca

Sample output 1

ca

Sample input 2

azxxzy

Sample output 2

ay

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.