Leetcode Problem 1081. Smallest Subsequence of Distinct Characters
1081. Smallest Subsequence of Distinct Characters
Leetcode Solutions
Monotonic Stack Approach
Create a stack to store the characters for the result subsequence.
Create a boolean array seen to indicate if a character is in the stack.
Create an array last to store the last occurrence index of each character.
Iterate through the characters of the string s.
If the current character is already in the stack, continue to the next iteration.
While the stack is not empty and the top character of the stack is greater than the current character and the top character appears later in the string, pop the stack and mark the character as not seen.
Push the current character onto the stack and mark it as seen.
Convert the stack to a string in reverse order to get the result.