← Home

1021. Remove Outermost Parentheses

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
102

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1021. Remove Outermost Parentheses, the solution in this repository is mainly a two pointers 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: two pointers, sliding window.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are removeOuterParentheses.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • 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:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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

solution.cpp
01//Runtime: 44 ms, faster than 5.97% of C++ online submissions for Remove Outermost Parentheses.
02//Memory Usage: 8.9 MB, less than 95.00% of C++ online submissions for Remove Outermost Parentheses.
03
04class Solution {
05public:
06    string removeOuterParentheses(string S) {
07        int left = -1, right = -1;
08        int balance = 0;
09        int i = 0;
10        while(i < S.size()){
11            if(S[i] == '('){
12                if(left == -1) left = i;
13                balance++;
14            }else if(S[i] == ')'){
15                balance--;
16            }
17            // cout << i << " " <<  S << " " <<  left << " " << right << " " << balance << endl;
18            if(balance == 0){
19                right = i;
20                // cout << i << " " <<  S << " " << left << " " << right << " " << balance << endl;
21                //remember to erase the latter character first!
22                //first erase right and then left!
23                S.erase(S.begin() + right);
24                S.erase(S.begin() + left);
25                left = -1;
26                right = -1;
27                //-2 for the erased '(' and ')'
28                //+1 for go next
29                i = i - 2 + 1;
30            }else{
31                i++;
32            }
33        }
34        return S;
35    }
36};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.