-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1081.cpp
More file actions
28 lines (28 loc) · 776 Bytes
/
Copy path1081.cpp
File metadata and controls
28 lines (28 loc) · 776 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 s) {
vector<char> st;
bool seen[26];
int mp[26];
memset(seen, 0, sizeof(seen));
int n = s.size();
for (int i = 0; i < n; ++i) {
mp[s[i] - 'a'] = i;
}
for (int i = 0; i < n; ++i) {
if (!seen[s[i] - 'a']) {
while (!st.empty() && st.back() - 'a' > s[i] - 'a' && mp[st.back() - 'a'] > i) {
seen[st.back() - 'a'] = false;
st.pop_back();
}
seen[s[i] - 'a'] = true;
st.push_back(s[i]);
}
}
string res;
for (auto& c : st) {
res.push_back(c);
}
return res;
}
};