Remove Duplicate Letters Problem

Remove Duplicate Letters Problem — ExecCode Hard DSA Practice

Solve the Remove Duplicate Letters problem on ExecCode. Free online hard DSA practice in Stack. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given a string s, remove duplicate letters so every distinct letter appears exactly once, and return the lexicographically smallest possible result. Use a monotonic stack. Before pushing a character, remove larger characters from the stack only if they still appear later, because then they can be placed after the current smaller character.

Examples

Input {"s": "bcabc"}; Output abc. Input {"s": "cbacdcbc"}; Output acdb. Input {"s": "bbcaac"}; Output bac

Constraints

1 <= len(s) <= 10^4 s contains lowercase English letters.

Practice Remove Duplicate Letters free on ExecCode. Browse DSA problems, topic map, and placement guides.