-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path316.cpp
More file actions
44 lines (34 loc) · 939 Bytes
/
316.cpp
File metadata and controls
44 lines (34 loc) · 939 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Problem : 316. Remove Duplicate Letters
// Link : https://leetcode.com/problems/remove-duplicate-letters/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
string removeDuplicateLetters(string s) {
int n = s.size();
vector<int> cnt(26, 0);
vector<bool>vis(26, false);
string ans = "";
for (int i = 0; i < n; i++)
cnt[s[i] - 'a']++;
for (char x : s) {
cnt[x - 'a']--;
if (! vis[x - 'a']) {
while (ans.size() && ans.back() > x && cnt[ans.back() - 'a']) {
vis[ans.back() - 'a'] = false;
ans.pop_back();
}
ans += x;
vis[x - 'a'] = true;
}
}
return ans;
}
};
int main() {
Solution ob;
cout << ob.removeDuplicateLetters("cbacdcbc");
return 0;
}