File tree Expand file tree Collapse file tree
algorithms/cpp/removeDuplicateLetters Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -62,6 +62,7 @@ LeetCode
6262| 322| [ Coin Change] ( https://leetcode.com/problems/coin-change/ ) | [ C++] ( ./algorithms/cpp/coinChange/coinChange.cpp ) | Medium|
6363| 321| [ Create Maximum Number] ( https://leetcode.com/problems/create-maximum-number/ ) | [ C++] ( ./algorithms/cpp/createMaximumNumber/CreateMaximumNumber.cpp ) | Hard|
6464| 319| [ Bulb Switcher] ( https://leetcode.com/problems/bulb-switcher/ ) | [ C++] ( ./algorithms/cpp/bulbSwitcher/bulbSwitcher.cpp ) | Medium|
65+ | 316| [ Remove Duplicate Letters] ( https://leetcode.com/problems/remove-duplicate-letters/ ) | [ C++] ( ./algorithms/cpp/removeDuplicateLetters/RemoveDuplicateLetters.cpp ) | Hard|
6566| 315| [ Count of Smaller Numbers After Self] ( https://leetcode.com/problems/count-of-smaller-numbers-after-self/ ) | [ C++] ( ./algorithms/cpp/countOfSmallerNumbersAfterSelf/countOfSmallerNumbersAfterSelf.cpp ) | Hard|
6667| 313| [ Super Ugly Number] ( https://leetcode.com/problems/super-ugly-number/ ) | [ C++] ( ./algorithms/cpp/superUglyNumber/SuperUglyNumber.cpp ) | Medium|
6768| 312| [ Burst Balloons] ( https://leetcode.com/problems/burst-balloons/ ) | [ C++] ( ./algorithms/cpp/burstBalloons/BurstBalloons.cpp ) | Hard|
Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/problems/remove-duplicate-letters/
2+ // Author : Hao Chen
3+ // Date : 2017-01-02
4+
5+ /* **************************************************************************************
6+ *
7+ * Given a string which contains only lowercase letters, remove duplicate letters so
8+ * that every letter appear once and only once. You must make sure your result is the
9+ * smallest in lexicographical order among all possible results.
10+ *
11+ * Example:
12+ *
13+ * Given "bcabc"
14+ * Return "abc"
15+ *
16+ * Given "cbacdcbc"
17+ * Return "acdb"
18+ *
19+ * Credits:Special thanks to @dietpepsi for adding this problem and creating all test
20+ * cases.
21+ ***************************************************************************************/
22+
23+
24+ class Solution {
25+ public:
26+ string removeDuplicateLetters (string s) {
27+ const int ASCII_LEN = 256 ;
28+ int counter[ASCII_LEN] = {0 };
29+ bool visited[ASCII_LEN] = {false };
30+
31+ for (char ch : s) {
32+ counter[ch]++;
33+ }
34+
35+ string result;
36+ for (char ch : s) {
37+ counter[ch]--;
38+ // if the current `ch` has already put into the result.
39+ if (visited[ch]) continue ;
40+
41+ // if the current `ch` is smaller than the last one char in result.
42+ // and we still have duplicated last-one char behind, so we can remove the current one.
43+ while ( !result.empty () && ch < result.back () && counter[result.back ()] ) {
44+ visited[result.back ()] = false ;
45+ result.pop_back ();
46+ }
47+ result.push_back (ch);
48+ visited[ch] = true ;
49+ }
50+ return result;
51+ }
52+ };
You can’t perform that action at this time.
0 commit comments