forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0316.cpp
More file actions
28 lines (26 loc) · 688 Bytes
/
0316.cpp
File metadata and controls
28 lines (26 loc) · 688 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 removeDuplicateLetters(string s)
{
int cnt[26];
memset(cnt, 0, sizeof cnt);
bool visited[26];
memset(visited, 0, sizeof visited);
for (char ch : s) cnt[ch - 'a']++;
string res;
for (char ch : s)
{
cnt[ch - 'a']--;
if (visited[ch - 'a']) continue;
while (res.size() and res.back() > ch and cnt[res.back() - 'a'] > 0)
{
visited[res.back() - 'a'] = false;
res.pop_back();
}
res.push_back(ch);
visited[ch - 'a'] = true;
}
return res;
}
};