Trains the technique from
LeetCode 316Remove Duplicate LettersThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A catalogue folds a raw code s of lowercase letters down to a label.
The label has to hold every distinct letter of s exactly once, and it has to be
reachable from s by deleting characters only, so the letters that survive stay
in the order they had in s.
More than one label can meet both rules. Return the one that comes first in dictionary order.
Example 1
The distinct letters are a, b, c and d. Deleting positions 0, 1 and 5 of "dbacdb" leaves "acdb", which holds each of them once.
Example 2
Both letters have to appear, and the only way to keep them in their original order is "ba".
Example 3
Only the letter z occurs, so the label is a single z.
Example 4
Deleting the c at position 1 leaves "bac", which holds a, b and c once each.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def remove_duplicate_letters(s: str) -> str:public String removeDuplicateLetters(String s)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.