Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 589 Bytes

1081. 不同字符的最小子序列.md

File metadata and controls

29 lines (21 loc) · 589 Bytes
function smallestSubsequence(s: string): string {

    const stack: string[] = [];

    for (let i = 0; i < s.length; ++i) {
        if (stack.includes(s[i])) {
            continue;
        }

        while (
            stack.length
            && stack[stack.length - 1] > s[i]
            && s.lastIndexOf(stack[stack.length - 1]) > i
        ) {
            stack.pop();
        }

        stack.push(s[i]);
    }

    return stack.join('');
};