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 193
193
1022|[ Sum of Root To Leaf Binary Numbers] ( ./1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
194
194
1037|[ Valid Boomerang] ( ./1037-valid-boomerang.js ) |Easy|
195
195
1041|[ Robot Bounded In Circle] ( ./1041-robot-bounded-in-circle.js ) |Medium|
196
+ 1081|[ Smallest Subsequence of Distinct Characters] ( ./1081-smallest-subsequence-of-distinct-characters.js ) |Medium|
196
197
1103|[ Distribute Candies to People] ( ./1103-distribute-candies-to-people.js ) |Easy|
197
198
1108|[ Defanging an IP Address] ( ./1108-defanging-an-ip-address.js ) |Easy|
198
199
1189|[ Maximum Number of Balloons] ( ./1189-maximum-number-of-balloons.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1081. Smallest Subsequence of Distinct Characters
3
+ * https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given a string s, return the lexicographically smallest subsequence
7
+ * of s that contains all the distinct characters of s exactly once.
8
+ */
9
+
10
+ /**
11
+ * @param {string } s
12
+ * @return {string }
13
+ */
14
+ var smallestSubsequence = 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