-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1081.cpp
More file actions
28 lines (28 loc) · 795 Bytes
/
1081.cpp
File metadata and controls
28 lines (28 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
string smallestSubsequence(string text) {
int n = text.size();
vector<int> idx(26,-1);
vector<bool> visited(26,false);
for(int i=0; i<n; i++){
idx[text[i]-'a'] = i;
}
deque<char> dq;
for(int i=0; i<n; i++){
while(!dq.empty() && !visited[text[i]-'a'] && text[i]<dq.back() && idx[dq.back()-'a']>i){
visited[dq.back()-'a'] = false;
dq.pop_back();
}
if(!visited[text[i]-'a']){
visited[text[i]-'a']= true;
dq.push_back(text[i]);
}
}
string res = "";
while(!dq.empty()){
res += dq.front();
dq.pop_front();
}
return res;
}
};