1
var smallestSubsequence = function (s) {
2
// Initialize an empty stack to store the result
3
let stack = [];
4

5
// Iterate through each character in the input string
6
for (let i = 0; i < s.length; i++) {
7
let char = s[i];
8

9
// If the character is already in the stack, skip it
10
if (stack.indexOf(char) > -1) continue;
11

12
// Check and pop elements from the stack to maintain lexicographical order
13
while (
14
stack.length > 0 &&
15
stack[stack.length - 1] > char &&
16
s.indexOf(stack[stack.length - 1], i) > i
17
) {
18
stack.pop();
19
}
20

21
// Push the current character onto the stack
22
stack.push(char);
23
}
24

25
// Join the characters in the stack to form the smallest subsequence
26
return stack.join("");
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0