100% Beat, C++ Solution using stack, Time and space complexity -O(n)
IntuitionThe problem requires us to remove the outermost parentheses of every valid primitive string in the input. A primitive string is a valid, non-empty pare
# Intuition
The problem requires us to remove the outermost parentheses of every valid primitive string in the input. A primitive string is a valid, non-empty parenthesis substring that cannot be split further.

The key observation is that the first and last parentheses of each valid primitive group should be removed. We can use a stack to track balance and determine which parentheses belong to the outermost layer.

# Approach
1. Initialize a stack to keep track of open parentheses.
2. Iterate through the string character by character:
    1. If the stack is empty, it means we are at the start of
       a  new primitive group, so we do not add the first '(' 
       to the result.
    2. Push '(' onto the stack when encountered.
    3. When encountering ')', pop an element from the stack:
        1. If the stack is still not empty after popping, append
           ')' to the result, since it is not an outer
           parenthesis.
3. The stack ensures that we only add characters that are not the first or last parentheses of a primitive group.
4. Return the final modified string.

# Complexity
- Time complexity:

We traverse the string once (O(n)) and perform constant-time operations inside the loop (O(1)).
Overall: O(n).

- Space Complexity:

The stack holds at most O(n) elements in the worst case. However, since we only use it to track valid parentheses and do not store extra large data, this can be considered O(1) auxiliary space (excluding input and output).
The answer string takes O(n) space in the worst case.
Overall: O(n) (for output storage).



# Code
```cpp []
class Solution {
public:
    string removeOuterParentheses(string s) {
        stack<char>stack;
        string ans;
        if(s.empty())return "";
        int n=s.length();
        for(int i=0;i<n;i++){
            if(stack.empty()) {
                stack.push(s[i]);
                continue;
            }
            if(s[i]=='('){
                stack.push('(');
            }
            if(s[i]==')'){
                stack.pop();
            }
            if(!stack.empty()) {
                
                ans+=s[i];
            }

        }
        return ans;

    }
};
```
['String', 'Stack', 'C++']