-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathremove_duplicate_letters.cpp
41 lines (37 loc) · 1.25 KB
/
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
/*
Leetcode Problem 316. Remove Duplicate Letters
Given a string s, remove duplicate letters so that every letter appears once and only once.
You must make sure your result is the smallest in lexicographical order among all possible results.
*/
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 ans = "";
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(ans.size() > 0 && ans.back() > s[i] && cnt[ans.back()-'a'] > 0)
{
//marking letter visited
vis[ans.back()-'a'] = 0;
ans.pop_back();
}
//add s[i] in ans and mark it visited
ans += s[i];
vis[s[i]-'a'] = 1;
}
}
return ans;
}
};