Skip to content

Commit c5f9ba8

Browse files
committedJan 2, 2023
Add solution #1081
1 parent d28baaa commit c5f9ba8

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
@@ -193,6 +193,7 @@
193193
1022|[Sum of Root To Leaf Binary Numbers](./1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
194194
1037|[Valid Boomerang](./1037-valid-boomerang.js)|Easy|
195195
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|
196197
1103|[Distribute Candies to People](./1103-distribute-candies-to-people.js)|Easy|
197198
1108|[Defanging an IP Address](./1108-defanging-an-ip-address.js)|Easy|
198199
1189|[Maximum Number of Balloons](./1189-maximum-number-of-balloons.js)|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.