Skip to content

Commit ae206e3

Browse files
authored
Update 316-remove-duplicate-letters.js
1 parent 2ef0925 commit ae206e3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

316-remove-duplicate-letters.js

+24
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,27 @@ const removeDuplicateLetters = function(s) {
5252
}
5353
return String.fromCharCode(...aChNo)
5454
}
55+
56+
// another
57+
58+
/**
59+
* @param {string} s
60+
* @return {string}
61+
*/
62+
const removeDuplicateLetters = function(s) {
63+
const last = {}
64+
for (let i = 0; i < s.length; i++) last[s.charAt(i)] = i
65+
const added = {}
66+
const stack = []
67+
for (let i = 0; i < s.length; i++) {
68+
const char = s.charAt(i)
69+
if (added[char]) continue
70+
while (stack.length && char < stack[stack.length - 1] && last[stack[stack.length - 1]] > i) {
71+
added[stack[stack.length - 1]] = false
72+
stack.pop()
73+
}
74+
stack.push(char)
75+
added[char] = true
76+
}
77+
return stack.join('')
78+
}

0 commit comments

Comments
 (0)