A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation.


	For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.


A valid parentheses string s is primitive if it is nonempty, and there does not exist a way to split it into s = A + B, with A and B nonempty valid parentheses strings.

Given a valid parentheses string s, consider its primitive decomposition: s = P1 + P2 + ... + Pk, where Pi are primitive valid parentheses strings.

Return s after removing the outermost parentheses of every primitive string in the primitive decomposition of s.

&nbsp;
Example 1:

Input: s = "(()())(())"
Output: "()()()"
Explanation: 
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".


Example 2:

Input: s = "(()())(())(()(()))"
Output: "()()()()(())"
Explanation: 
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".


Example 3:

Input: s = "()()"
Output: ""
Explanation: 
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 105
	s[i] is either '(' or ')'.
	s is a valid parentheses string.


class Solution:
    def removeOuterParentheses(self, s: str) -> str:
        c=0
        res=''
        for i in s:
            if i==')' and c==1:
                c=c-1
            elif i=='(' and c==0:
                c=c+1
            elif i=='(':
                res=res+'('
                c=c+1
            elif i==')':
                res=res+')'
                c=c-1
        return res
class Solution {
    public String removeOuterParentheses(String s) {
    // if '(' check stack size > 0 add ans else not add ans
        // if ')' check stack size > 0 add ans else not add ans
        Stack<Character> st = new Stack<>();
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<s.length();i++){
            char ch = s.charAt(i);

            if(ch == '('){
                if(st.size() > 0){
                    sb.append(ch);
                }
                st.push(ch);
            }

            else{
                st.pop();
                if(st.size() > 0){
                    sb.append(ch);
                }
            }
        }
        return sb.toString();
    }
}
class Solution {
public:
    stack<char>p;
    int count=0;
    string removeOuterParentheses(string s) {
        string ans={};

        for(auto &i:s){
            if(i=='(' && count==0){
                p.push(i);
                count++;
            }
            else if (i=='(' && count!=0){
                ans+='(';
                p.push(i);
                count++;
            }
            else{
                count--;
                p.pop();
                if(count>0) ans+=')';
            }
            
        }
        return ans;
    }
};
var removeOuterParentheses = function(s) {
    let open = -1, ans = "", stack = [];
    for(let c of s) {
        if(c == '(') {
            // open for top level open
            if(open != -1) ans += c;
            stack.push(open)
            open++;
        } else {
            open = stack.pop();
            // close for top level open
            if(open != -1) ans += c;
        }
    }
    return ans;
};
Remove Outermost Parentheses