-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0316-Remove_Duplicate_Letters.cpp
More file actions
100 lines (91 loc) · 2.34 KB
/
0316-Remove_Duplicate_Letters.cpp
File metadata and controls
100 lines (91 loc) · 2.34 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*******************************************************************************
* 0316-Remove_Duplicate_Letters.cpp
* Billy.Ljm
* 26 September 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/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.
*
* ===========
* My Approach
* ===========
* If a letter is the last occurrence of the character, then we have to include
* it. Otherwise, if it is not the last occurrence, then we add it temporarily.
* However, we might have to pop this characters out and append the later
* occurrences if a lexicographically smaller character comes along.
*
* This has a time complexity of O(n^2), and a space complexity of O(n), where n
* is the length of the string.
******************************************************************************/
#include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
public:
string removeDuplicateLetters(string s) {
// count letters
map<char, int> count;
for (char c : s) {
if (count.find(c) != count.end()) count[c]++;
else count[c] = 1;
}
// form output
string out;
set<char> visited;
for (char c : s) {
// append any new chars
if (visited.find(c) == visited.end()) {
// remove any lexographically larger chars with later occurences
while (out.size() > 0 and out.back() > c and count[out.back()] != 0) {
visited.erase(out.back());
out.pop_back();
}
out += c;
visited.insert(c);
}
// update remaining count
count[c]--;
}
return out;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
string s;
// test case 1
s = "bcabc";
std::cout << "removeDuplicateLetters(" << s << ") = ";
std::cout << sol.removeDuplicateLetters(s) << std::endl;
// test case 2
s = "cbacdcbc";
std::cout << "removeDuplicateLetters(" << s << ") = ";
std::cout << sol.removeDuplicateLetters(s) << std::endl;
return 0;
}