615. Remove Duplicate Letters

MediumStackMonotonic StackGreedyString

Given a string s, remove duplicate letters so that every letter appears exactly once, and return the lexicographically smallest result among all such possibilities. The input is JSON {s}.

Input: JSON {s}.

Output: String — the smallest de-duplicated string.

Examples

Example 1
Input: {"s":"cbacdcbc"}
Output: acdb
Explanation: Smallest string with each letter once.
Example 2
Input: {"s":"bcabc"}
Output: abc
Explanation: Each letter once, smallest order.
Example 3
Input: {"s":"a"}
Output: a
Explanation: Single letter.

Constraints

Asked by

PaytmMicrosoftAmazonGoogleBloombergMeta
Solve this problem in the editor →