✅ C++ 2 solutions: stack & counter
Solution 1 : Use std::stackIntuitionTo solve this problem, we can keep track of the outer parentheses using a stack. Whenever we encounter an open parenthesis,
# Solution 1 : Use std::stack

## Intuition
To solve this problem, we can keep track of the outer parentheses using a stack. Whenever we encounter an open parenthesis, we push it onto the stack. When we encounter a close parenthesis, we pop an open parenthesis from the stack. We only add the current character to our result string when the stack is not empty.

## Approach
1 Initialize an empty stack and an empty result string.
2. Iterate through the input string:
a. If the stack is not empty, add the current character to the result string.
b. If the current character is an open parenthesis, push it onto the stack.
c. If the current character is a close parenthesis, pop an open parenthesis from the stack. If the stack is now empty, remove the last character from the result string.
3. Return the result string.

## Complexity
- Time complexity: $$O(n)$$, where n is the length of the input string. We iterate through the entire string once.
- Space complexity: $$O(n)$$, as in the worst case (e.g., all open parentheses), the stack could store all characters in the input string.

# Code
```
class Solution {
public:
    string removeOuterParentheses(string s) {
        std::stack<char> stack;
        std::string r;

        for(auto c: s){
            if(!stack.empty()) {
                r += c;
            }
            if(c == '(') {
                stack.push(c);
            }
            else {
                stack.pop();
                if(stack.empty()) { 
                    r.pop_back();
                }
            }
        }

        return r;
    }
};
```


# Solution 2 : Use a counter

## Intuition
An alternative to using a stack is to simply maintain a count of the open parentheses encountered. We can increment the count when we encounter an open parenthesis and decrement it when we encounter a close parenthesis.

## Approach
1. Initialize an empty result string and a count variable set to 0.
2. Iterate through the input string:
a. If the current character is an open parenthesis and the count is greater than 0, add the current character to the result string and increment the count.
b. If the current character is a close parenthesis and the count is greater than 1, add the current character to the result string and decrement the count.
3. Return the result string.

## Complexity
- Time complexity: $$O(n)$$, where n is the length of the input string. We iterate through the entire string once.
- Space complexity: $$O(n)$$, since the result string could store up to n-2 characters (e.g., if the input string has only one pair of outer parentheses).

# Code
```
class Solution {
public:
    string removeOuterParentheses(string s) {
        std::string r;
        int count = 0;
        for(char c : s){
            if(c == '(' && count++ > 0) r += c;
            if(c == ')' && count-- > 1) r += c;
        }
        return r;
    }
};
```
['C', 'C++']