Problem Description
Given a string s, return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.
Key Insights
- The result must contain all distinct characters from the input string.
- The order of characters in the result should be lexicographically smallest.
- A greedy approach can be used to build the result by ensuring that at each step, the smallest possible character is selected.
- A stack can help maintain the order of characters while ensuring that duplicates are not added.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the input string s. Each character is processed a limited number of times. Space Complexity: O(1), since the maximum number of distinct characters is limited (26 lowercase English letters).
Solution
We can use a stack to build the result string. The algorithm maintains a set to track the characters that are in the stack. For each character in the string, we check if it can be added to the stack. If the current character is smaller than the character at the top of the stack and the top character appears later in the string, we pop the stack. This ensures that we always maintain the lexicographical order. Once done, we concatenate the characters in the stack to form the final result.