Easy Solution with Dry Run and Example
Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve this, we need to:\n\n1. Traverse the string while keeping track of the number 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve this, we need to:\n\n1. Traverse the string while keeping track of the number of open and close parentheses using a counter.\n \n1. Append the parentheses to the result string only when they are not the outermost ones.\n\n---\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Initialization**:\n\n- Start with an empty result string ans.\n- Use a counter cnt initialized to 0 to keep track of the balance of open and close parentheses.\n\n2. **Traversing the String:**\n\n- Iterate through each character in the string s.\n\n- For each \'(\':\n    - If cnt is 0, it indicates this \'(\' is an outer parenthesis, so we increment cnt without adding it to ans.\n    - Otherwise, increment cnt and add the parenthesis to ans.\n     \n- For each \')\':\n    - If cnt is 1, it indicates this \')\' is an outer parenthesis, so we decrement cnt without adding it to ans.\n    - Otherwise, decrement cnt and add the parenthesis to ans.\nReturning the Result:\n\nThe string ans now contains the input string s without its outermost parentheses.\n\n---\n\n\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n) -> for ans string which is required!\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n\n# Dry Run\n**Consider the input string s = "(()())(())":**\n\n- The first primitive string is "(()())":\n    - The outermost parentheses are removed, resulting in "()()".\n- The second primitive string is "(())":\n    - The outermost parentheses are removed, resulting in "()".\n- Concatenate these results to get "()()()".\n\n---\n\n\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    string removeOuterParentheses(string s) {\n       string ans = "";\n       int cnt = 0;\n       for(int i=0;i<s.size();i++){\n            if(cnt ==0 && s[i]==\'(\') cnt++;\n            else if(s[i]==\'(\'){\n                cnt++;\n                ans+=s[i];\n            }\n            if(cnt == 1 && s[i]==\')\') cnt--;\n            else if(s[i]==\')\'){\n                cnt--;\n                ans+=s[i];\n            }\n       }\n       return ans; \n    }\n};\n```\n\n```javascript []\nfunction removeOuterParentheses(s) {\n    let ans = "";\n    let cnt = 0;\n\n    for (let i = 0; i < s.length; i++) {\n        if (cnt === 0 && s[i] === \'(\') {\n            cnt++;\n        } else if (s[i] === \'(\') {\n            cnt++;\n            ans += s[i];\n        } else if (cnt === 1 && s[i] === \')\') {\n            cnt--;\n        } else {\n            cnt--;\n            ans += s[i];\n        }\n    }\n\n    return ans;\n}\n\n```\n```python []\ndef removeOuterParentheses(s: str) -> str:\n    ans = ""\n    cnt = 0\n\n    for char in s:\n        if cnt == 0 and char == \'(\':\n            cnt += 1\n        elif char == \'(\':\n            cnt += 1\n            ans += char\n        elif cnt == 1 and char == \')\':\n            cnt -= 1\n        else:\n            cnt -= 1\n            ans += char\n\n    return ans\n\n```\n```java []\npublic class Solution {\n    public String removeOuterParentheses(String s) {\n        StringBuilder ans = new StringBuilder();\n        int cnt = 0;\n\n        for (int i = 0; i < s.length(); i++) {\n            if (cnt == 0 && s.charAt(i) == \'(\') {\n                cnt++;\n            } else if (s.charAt(i) == \'(\') {\n                cnt++;\n                ans.append(s.charAt(i));\n            } else if (cnt == 1 && s.charAt(i) == \')\') {\n                cnt--;\n            } else {\n                cnt--;\n                ans.append(s.charAt(i));\n            }\n        }\n\n        return ans.toString();\n    }\n}\n\'\n```\n![649768ad8f25f.jpeg](https://assets.leetcode.com/users/images/a0a6f80c-9ed6-4608-91b1-e1359eec3396_1724518832.8392153.jpeg)\n
['String', 'Python', 'C++', 'Java', 'JavaScript']