-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path316.cpp
More file actions
27 lines (25 loc) · 757 Bytes
/
316.cpp
File metadata and controls
27 lines (25 loc) · 757 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
class Solution {
public:
string removeDuplicateLetters(string s) {
vector<char> st;
unordered_set<char> seen;
unordered_map<char, int> mp;
int n = s.size();
for (int i = 0; i < n; ++i) mp[s[i]] = i;
for (int i = 0; i < n; ++i) {
char c = s[i];
if (seen.find(c) == seen.end()) {
while (!st.empty() && c < st.back() && i < mp[st.back()]) {
char temp = st.back();
st.pop_back();
seen.erase(temp);
}
seen.insert(c);
st.push_back(c);
}
}
string out;
for (auto& c : st) out.push_back(c);
return out;
}
};