Skip to content

Commit 8bb7c80

Browse files
authored
Create 1081.Smallest-Subsequence-of-Distinct-Characters.cpp
1 parent 13f2082 commit 8bb7c80

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
string smallestSubsequence(string text)
4+
{
5+
string str;
6+
vector<int>count(26);
7+
for (auto ch: text)
8+
count[ch-'a']++;
9+
unordered_set<char>visited;
10+
11+
for (auto ch: text)
12+
{
13+
if (visited.find(ch)!=visited.end())
14+
{
15+
count[ch-'a']--;
16+
}
17+
else
18+
{
19+
while (str.size()>0 && str.back()>ch && count[str.back()-'a']>0)
20+
{
21+
visited.erase(str.back());
22+
str.pop_back();
23+
}
24+
str.push_back(ch);
25+
count[ch-'a']--;
26+
visited.insert(ch);
27+
}
28+
}
29+
30+
return str;
31+
}
32+
};

0 commit comments

Comments
 (0)