Let's make this one less mysterious. For 1047. Remove All Adjacent Duplicates In String, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are removeDuplicates.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 44 ms, faster than 28.17% of C++ online submissions for Remove All Adjacent Duplicates In String.
02//Memory Usage: 11.5 MB, less than 100.00% of C++ online submissions for Remove All Adjacent Duplicates In String.
03
04class Solution {
05public:
06 string removeDuplicates(string S) {
07 int nRemoved = 0;
08
09 do{
10 nRemoved = 0;
11 for(int i = 0; i < S.size() - 1;){
12 if(S[i] == S[i+1]){
13 //remove from i, length 2
14 // cout << S.size() << ", " << i << endl;
15 S.erase(i, 2);
16 i--;
17 nRemoved++;
18 }else{
19 i++;
20 }
21 }
22 }while(nRemoved > 0 && S.size() > 0);
23
24 return S;
25 }
26};
27
28//Two pointer:
29//https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/294893/JavaC%2B%2BPython-Two-Pointers-and-Stack-Solution
30//Runtime: 20 ms, faster than 82.26% of C++ online submissions for Remove All Adjacent Duplicates In String.
31//Memory Usage: 11.8 MB, less than 100.00% of C++ online submissions for Remove All Adjacent Duplicates In String.
32
33class Solution {
34public:
35 string removeDuplicates(string S) {
36 int isrc, idst;
37 for(isrc = 0, idst = 0; isrc < S.size(); isrc++, idst++){
38 S[idst] = S[isrc];
39 if(idst > 0 && S[idst] == S[idst-1]){
40 idst -= 2;
41 }
42 }
43 return S.substr(0, idst);
44 }
45};
46
47//Stack
48//https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/294893/JavaC%2B%2BPython-Two-Pointers-and-Stack-Solution
49//Runtime: 16 ms, faster than 90.84% of C++ online submissions for Remove All Adjacent Duplicates In String.
50//Memory Usage: 12.6 MB, less than 100.00% of C++ online submissions for Remove All Adjacent Duplicates In String.
51
52class Solution {
53public:
54 string removeDuplicates(string S) {
55 string ans = "";
56 for(char c : S){
57 if(ans.size() > 0 && c == ans.back()){
58 ans.pop_back();
59 }else{
60 ans.push_back(c);
61 }
62 }
63 return ans;
64 }
65};
Cost