-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay-11-Remove Duplicate Letters.cpp
51 lines (36 loc) · 1.15 KB
/
Day-11-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
46
47
48
49
50
51
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.
Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
1 <= s.length <= 104
s consists of lowercase English letters.
class Solution {
public:
string removeDuplicateLetters(string s) {
vector<int> count(26, 0);
for (char c : s) {
count[c - 'a'] += 1;
}
string res = "";
vector<bool> used(26, false);
for (char c : s) {
if (used[c - 'a']) {
count[c - 'a'] -= 1;
continue;
}
while (!res.empty() and c < res.back() and count[res.back() - 'a'] > 0) {
used[res.back() - 'a'] = false;
res.pop_back();
}
res.push_back(c);
used[c - 'a'] = true;
count[c - 'a'] -= 1;
}
return res;
}
};