-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
49 lines (44 loc) · 968 Bytes
/
main.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
string removeDuplicateLetters(string s) {
vector<int> count(30, 0);
vector<bool> seen(30, false);
for (char c: s) {
count[c - 'a']++;
}
string ans = "";
for (char c: s) {
int index = c - 'a';
count[index]--;
if (seen[index]) {
continue;
}
while (!ans.empty() && ans.back() > c && count[ans.back() - 'a'] > 0) {
seen[ans.back() - 'a'] = false;
ans.pop_back();
}
ans.push_back(c);
seen[index] = true;
}
return ans;
}
};
int main() {
Solution sol;
cout << sol.removeDuplicateLetters("bcabc") << endl;
cout << sol.removeDuplicateLetters("cbacdcbc") << endl;
return 0;
}