Remove Outermost Parenthesis || Beats 100% || Java || Fully Explained
Approach\n- The method starts by initializing a StringBuilder to construct the final result. Declare an int variable cnt to keep track of the number of open par
# Approach\n- The method starts by initializing a StringBuilder to construct the final result. Declare an int variable `cnt` to keep track of the number of open parentheses encountered.\n- Iterate through each character of string s. Within the loop, the code checks if the current character is an opening parenthesis `\'(\'`.\n- If an opening parenthesis is encountered, check if there are already open parentheses (nested ones). If yes, add the current opening parenthesis to the result `(sb)`. Increment the count of open parentheses `(cnt)`.\n- If a closing parenthesis is encountered, check if there are still open parentheses (nested ones). If yes, add the current closing parenthesis to the result `(sb)`. Decrement the count of open parentheses` (cnt)`.\n- Return the result (sb).\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)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public String removeOuterParentheses(String s) {\n        StringBuilder sb = new StringBuilder();\n        int cnt = 0;\n\n        for (char c : s.toCharArray()) {\n            if (c == \'(\') {\n                if (cnt > 0) {\n                    sb.append(c);\n                }\n                cnt++;\n            } else if (c == \')\') {\n                cnt--;\n                if (cnt > 0) {\n                    sb.append(c);\n                }\n            }\n        }\n\n        return sb.toString();\n    }\n}\n\n```
['String', 'Java']