Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f42347d

Browse files
authoredJun 9, 2019
Create 5086-smallest-subsequence-of-distinct-characters.js
1 parent 0730190 commit f42347d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string} text
3+
* @return {string}
4+
*/
5+
const smallestSubsequence = function(text) {
6+
if (text === '') return ''
7+
let counter = new Array(128).fill(0)
8+
for (let i = 0; i < text.length; i++) counter[text.charCodeAt(i)]++
9+
let minChar = 128
10+
let minIndex = 0
11+
for (let i = 0; i < text.length; i++) {
12+
let c = text.charCodeAt(i)
13+
if (c < minChar) {
14+
minChar = c
15+
minIndex = i
16+
}
17+
if (--counter[c] === 0) {
18+
return (
19+
String.fromCharCode(minChar) +
20+
smallestSubsequence(
21+
text
22+
.slice(minIndex + 1)
23+
.replace(new RegExp(String.fromCharCode(minChar), 'g'), '')
24+
)
25+
)
26+
}
27+
}
28+
return ''
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.