File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 114
114
278|[ First Bad Version] ( ./0278-first-bad-version.js ) |Medium|
115
115
283|[ Move Zeroes] ( ./0283-move-zeroes.js ) |Easy|
116
116
290|[ Word Pattern] ( ./0290-word-pattern.js ) |Easy|
117
+ 316|[ Remove Duplicate Letters] ( ./0316-remove-duplicate-letters.js ) |Medium|
117
118
326|[ Power of Three] ( ./0326-power-of-three.js ) |Easy|
118
119
342|[ Power of Four] ( ./0342-power-of-four.js ) |Easy|
119
120
344|[ Reverse String] ( ./0344-reverse-string.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments