240. Remove Duplicate Letters

MediumStringString

Given a string s, remove duplicate letters so that every letter appears exactly once. Return the lexicographically smallest result among all valid orderings that preserve relative feasibility.

Input: A quoted string s.

Output: Quoted string — the smallest unique-letter string.

Examples

Example 1
Input: "bcabc"
Output: "abc"
Explanation: Smallest with each letter once.
Example 2
Input: "cbacdcbc"
Output: "acdb"
Explanation: Smallest valid ordering.
Example 3
Input: "abc"
Output: "abc"
Explanation: Already unique.

Constraints

Asked by

PaytmMicrosoftAmazonGoogleBloombergMeta
Solve this problem in the editor →