InferenceAI

Enter at least two characters to search.

Start free

Smallest Unique-Letter Subsequence

hardStack40 min50 XP

Remove duplicate letters from s so each letter appears exactly once, and the result is the lexicographically smallest possible subsequence of s.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT["bcabc"]
TASKsmallest_unique_letters()
EXPECTED OUTPUT"abc"

Start with the input values shown in this example.

Example 1 of 3

Input: s = "bcabc"

Output: "abc"

Explanation: classic

Input: s = "cbacdcbc"

Output: "acdb"

Explanation: longer

Input: s = "a"

Output: "a"

Explanation: single

Hint 1

Keep a stack. Pop a larger letter from the top if it appears again later.

Hint 2

Skip a letter that's already in the stack.

Topics

DSAStackPython