Skip to content
Katabench
Try free

Remove All Adjacent Duplicates

Easy About 10 min Free, no account needed

Keep deleting adjacent equal pairs until none remain.

The brief

Given a lowercase string s, repeatedly delete two adjacent equal letters until no such pair remains, and return the result (it is unique regardless of deletion order).

The performance bar

The obvious approach rescans and rebuilds the string after every deletion pass, O(n2) on inputs that collapse layer by layer, like the hidden 40,000-character "onion" that annihilates completely, so the repeated passes are too slow. You will need a single-pass approach, O(n).

Starter code

C#
using System;

public class Solution
{
    // TODO: return the string after all adjacent equal pairs are (repeatedly) removed.
    public string RemoveDuplicates(string s)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

Stacks
Use last-in, first-out state for nesting, matching and expression evaluation.

Related katas