-
Notifications
You must be signed in to change notification settings - Fork 0
/
316.cpp
46 lines (41 loc) · 980 Bytes
/
316.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
/**
* Remove duplicate letters
*
* */
#include <string>
#include <array>
#include <iostream>
class Sloution
{
public:
std::string removeDuplicateLetters(const std::string& s)
{
std::array<int, 26> times;
std::array<bool, 26> cand;
std::array<int, 26> a;
times.fill(0);
cand.fill(false);
for (auto&& c: s) {
times[c - 'a']++;
}
std::string res;
for (int i = 0; i < s.size(); ++i) {
times[s[i] - 'a']--;
if (cand[s['i'] - 'a']) {
continue;
}
while (!res.empty() && times[res.back() - 'a'] >= 1 && s[i] < res.back()) {
cand[res.back() - 'a'] = false;
res.pop_back();
}
res.push_back(s[i]);
cand[s[i] - 'a'] = true;
}
return res;
}
};
int main()
{
Sloution s;
std::cout << s.removeDuplicateLetters("bcabc") << std::endl;
}