-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy path0316. Remove Duplicate Letters.cpp
45 lines (39 loc) · 1.15 KB
/
0316. Remove Duplicate Letters.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
// 856.✅ Score of Parentheses
class Solution
{
public:
string removeDuplicateLetters(string s)
{
// cnt for storing frequency of characters
// vis for marking visited characters
vector<int> cnt(26, 0), vis(26, 0);
string res = "";
int n = s.size();
for (int i = 0; i < n; ++i)
cnt[s[i] - 'a']++;
for (int i = 0; i < n; ++i)
{
// decrease cnt of current character
cnt[s[i] - 'a']--;
// If character is not already
// in answer
if (!vis[s[i] - 'a'])
{
// Last character > s[i]
// and its count > 0
while (res.size() > 0 && res.back() > s[i] && cnt[res.back() - 'a'] > 0)
{
// marking letter visited
vis[res.back() - 'a'] = 0;
res.pop_back();
}
// Add s[i] in res and
// mark it visited
res += s[i];
vis[s[i] - 'a'] = 1;
}
}
// return resultant string
return res;
}
};