Skip to content

Commit 27f37c2

Browse files
authored
Update 1081-smallest-subsequence-of-distinct-characters.js
1 parent 3794274 commit 27f37c2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1081-smallest-subsequence-of-distinct-characters.js

+23
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,26 @@ const smallestSubsequence = function(s) {
5656
return res.join('')
5757
};
5858

59+
// anoother
60+
61+
62+
/**
63+
* @param {string} text
64+
* @return {string}
65+
*/
66+
const smallestSubsequence = function(text) {
67+
const n = text.length, stack = [], last = {}, visited = {}
68+
for(let i = 0; i < n; i++) last[text[i]] = i
69+
for(let i = 0; i < n; i++) {
70+
const ch = text[i]
71+
if (visited[ch]) continue
72+
while(stack.length && stack[stack.length - 1] > ch && last[stack[stack.length - 1]] > i) {
73+
visited[stack[stack.length - 1]] = 0
74+
stack.pop()
75+
}
76+
visited[ch] = 1
77+
stack.push(ch)
78+
}
79+
80+
return stack.join('')
81+
};

0 commit comments

Comments
 (0)