✅C++|| Stack And Without Stack || Easy Solution
Approach 1: (Stack) \u2705\n\nC++\n\nclass Solution {\npublic:\n\nstring removeOuterParentheses(string S) {\n    stack<char>st;\n    string ans;\n    for(auto a
**Approach 1: (Stack) \u2705**\n\n**C++**\n```\nclass Solution {\npublic:\n\nstring removeOuterParentheses(string S) {\n    stack<char>st;\n    string ans;\n    for(auto a:S)\n    {\n        if(a==\'(\')\n        {\n            if(st.size()>0)\n            {\n                ans+=\'(\';\n            }\n            st.push(\'(\');\n        }\n        else\n        {\n            if(st.size()>1)\n            {\n                ans+=\')\';\n            }\n            st.pop();\n        }\n    }\n    return ans;\n}\n};\n```\n**Java**\n```\nclass Solution {\n    public String removeOuterParentheses(String s) {\n        Stack<Character> st = new Stack<>();\n        StringBuilder sb = new StringBuilder();\n        \n        for(int i=0;i<s.length();i++){\n            char ch = s.charAt(i);\n            \n            if(ch == \'(\'){\n                if(st.size() > 0){\n                    sb.append(ch);\n                }\n                st.push(ch);\n            }\n            else\n            {\n               st.pop();\n                if(st.size() > 0){\n                    sb.append(ch);\n                }\n            }\n        }\n        return sb.toString();\n    }\n}\n```\n\n**Approach 2: (Without Stack ) \u2705**\n\n**C++**\n```\nclass Solution {\npublic:\n\nstring removeOuterParentheses(string S) {\n    int st=0;\n    string ans;\n    for(auto a:S)\n    {\n        if(a==\'(\')\n        {\n            if(st>0)\n            {\n                ans+=\'(\';\n            }\n            st++;\n        }\n        else\n        {\n            if(st>1)\n            {\n                ans+=\')\';\n            }\n            st--;\n        }\n    }\n    return ans;\n}\n};\n```\n**Java**\n```\nclass Solution {\n    public String removeOuterParentheses(String S) {\n        StringBuilder sb = new StringBuilder();\n        int counter = 0;\n        for(char c : S.toCharArray()){\n            if(c == \'(\'){\n                if(counter != 0) sb.append(c);\n                counter++;\n            }\n            else{\n                counter--;\n                if(counter != 0) sb.append(c);\n            }\n        }\n        \n        return sb.toString();\n    }\n}\n```\n
['String', 'Stack', 'C']