Skip to content

Commit d28baaa

Browse files
committed
Add solution #316
1 parent ee8de72 commit d28baaa

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
278|[First Bad Version](./0278-first-bad-version.js)|Medium|
115115
283|[Move Zeroes](./0283-move-zeroes.js)|Easy|
116116
290|[Word Pattern](./0290-word-pattern.js)|Easy|
117+
316|[Remove Duplicate Letters](./0316-remove-duplicate-letters.js)|Medium|
117118
326|[Power of Three](./0326-power-of-three.js)|Easy|
118119
342|[Power of Four](./0342-power-of-four.js)|Easy|
119120
344|[Reverse String](./0344-reverse-string.js)|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 316. Remove Duplicate Letters
3+
* https://leetcode.com/problems/remove-duplicate-letters/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is
7+
* the smallest in lexicographical order among all possible results.
8+
*/
9+
10+
/**
11+
* @param {string} s
12+
* @return {string}
13+
*/
14+
var removeDuplicateLetters = function(s) {
15+
const stack = [];
16+
17+
for (let i = 0; i < s.length; i++) {
18+
const letter = s[i];
19+
if (stack.indexOf(letter) > -1) {
20+
continue;
21+
}
22+
while (stack.length > 0 && stack[stack.length - 1] > letter && s.indexOf(stack[stack.length - 1], i) > i) {
23+
stack.pop();
24+
}
25+
stack.push(letter);
26+
}
27+
28+
return stack.join('');
29+
};

0 commit comments

Comments
 (0)