Skip to content

Commit 2ef0925

Browse files
authored
Update 1316-distinct-echo-substrings.js
1 parent 7bf6cf7 commit 2ef0925

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1316-distinct-echo-substrings.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,27 @@ const distinctEchoSubstrings = function (text) {
1717
}
1818
return set.size
1919
}
20+
21+
// another
22+
23+
/**
24+
* @param {string} s
25+
* @return {string}
26+
*/
27+
const removeDuplicateLetters = function(s) {
28+
const last = {}
29+
for (let i = 0; i < s.length; i++) last[s.charAt(i)] = i
30+
const added = {}
31+
const stack = []
32+
for (let i = 0; i < s.length; i++) {
33+
const char = s.charAt(i)
34+
if (added[char]) continue
35+
while (stack.length && char < stack[stack.length - 1] && last[stack[stack.length - 1]] > i) {
36+
added[stack[stack.length - 1]] = false
37+
stack.pop()
38+
}
39+
stack.push(char)
40+
added[char] = true
41+
}
42+
return stack.join('')
43+
}

0 commit comments

Comments
 (0)