Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example 1:
Input: s = "bcabc" Output: "abc"
Example 2:
Input: s = "cbacdcbc" Output: "acdb"
Constraints:
1 <= s.length <= 104s consists of lowercase English letters.Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a jumbled bag of letter tiles and want to spell out the shortest possible word that uses every unique letter at least once, but in the most alphabetically pleasing order. The brute force approach works by generating every single combination of the letters, checking which ones contain all the unique characters, and then sorting through those valid results to find the one that comes first in the alphabet.
Here's how the algorithm would work step-by-step:
The optimal strategy is like building a collection of unique items one by one while keeping them in alphabetical order. We carefully look at each letter in order, making sure we only keep the best possible arrangement by replacing earlier choices if a better letter comes along later and our previous choices still appear further down the line.
Here's how the algorithm would work step-by-step:
def remove_duplicate_letters(input_message):
last_seen_indices = {}
for index, character in enumerate(input_message):
last_seen_indices[character] = index
# We track visited characters to ensure each unique letter appears only once.
visited_characters = set()
selected_letter_stack = []
for index, character in enumerate(input_message):
if character not in visited_characters:
# We pop larger characters to maintain alphabetical order if they appear later.
while selected_letter_stack and selected_letter_stack[-1] > character and last_seen_indices[selected_letter_stack[-1]] > index:
removed_character = selected_letter_stack.pop()
visited_characters.remove(removed_character)
selected_letter_stack.append(character)
visited_characters.add(character)
# The final string is assembled from our stack of optimally chosen letters.
return "".join(selected_letter_stack)| Case | How to Handle |
|---|---|
| Empty string or single character string | Return the input string directly as no duplicates can exist and order is already optimal. |
| String with all identical characters | Keep only the first or last occurrence by utilizing a tracking mechanism that drops previous identical characters if a better lexicographical arrangement is found. |
| String with already unique characters in strictly increasing order | Iterate through the string without removing any characters since the original order is already the lexicographically smallest. |
| String with unique characters in strictly decreasing order | The monotonic stack mechanism will pop larger preceding characters in favor of smaller incoming characters, ultimately retaining only the final occurrence of each. |
| Characters appearing multiple times separated by other characters | Use a last occurrence frequency map to ensure a character is only popped from the stack if it appears again later in the string. |
| Large input string approaching maximum constraints | Process the string in a single linear pass using a stack and frequency array to achieve optimal O(N) time complexity. |
| Characters already present in the result stack | Maintain a boolean visited set to immediately skip characters that have already been included in the final result. |