-
Notifications
You must be signed in to change notification settings - Fork 227
/
1081-smallest-subsequence-of-distinct-characters.js
81 lines (75 loc) · 1.87 KB
/
1081-smallest-subsequence-of-distinct-characters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @param {string} text
* @return {string}
*/
const smallestSubsequence = function(text) {
if (text === '') return ''
let counter = new Array(128).fill(0)
for (let i = 0; i < text.length; i++) counter[text.charCodeAt(i)]++
let minChar = 128
let minIndex = 0
for (let i = 0; i < text.length; i++) {
let c = text.charCodeAt(i)
if (c < minChar) {
minChar = c
minIndex = i
}
if (--counter[c] === 0) {
return (
String.fromCharCode(minChar) +
smallestSubsequence(
text
.slice(minIndex + 1)
.replace(new RegExp(String.fromCharCode(minChar), 'g'), '')
)
)
}
}
return ''
}
// another
/**
* @param {string} text
* @return {string}
*/
const smallestSubsequence = function(s) {
let res = []
const count = new Array(26).fill(0)
const used = new Array(26).fill(0)
const aCode = 'a'.charCodeAt(0)
for (let el of s) count[el.charCodeAt(0) - aCode]++
for (let el of s) {
count[el.charCodeAt(0) - aCode]--
if (used[el.charCodeAt(0) - aCode]++ > 0) continue
while (
res.length &&
res[res.length - 1].charCodeAt(0) > el.charCodeAt(0) &&
count[res[res.length - 1].charCodeAt(0) - aCode] > 0
) {
used[res[res.length - 1].charCodeAt(0) - aCode] = 0
res.pop()
}
res.push(el)
}
return res.join('')
};
// anoother
/**
* @param {string} text
* @return {string}
*/
const smallestSubsequence = function(text) {
const n = text.length, stack = [], last = {}, visited = {}
for(let i = 0; i < n; i++) last[text[i]] = i
for(let i = 0; i < n; i++) {
const ch = text[i]
if (visited[ch]) continue
while(stack.length && stack[stack.length - 1] > ch && last[stack[stack.length - 1]] > i) {
visited[stack[stack.length - 1]] = 0
stack.pop()
}
visited[ch] = 1
stack.push(ch)
}
return stack.join('')
};