Beats 100% 🔥 of users|| JAVA  || Without Using Stack || Easy to understand ✅
Intuition\n Describe your first thoughts on how to solve this problem. \nRemove the outermost parentheses from each primitive valid parentheses substring by tra
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRemove the outermost parentheses from each primitive valid parentheses substring by tracking balance.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1) Initialize: Convert the string to a character array, create a StringBuilder, and initialize a count for balance.\n\n2) Process Characters:\nTraverse from the second character.\nFor each \'(\', increase the balance and add it to the result.\nFor each \')\', add it to the result if balance is greater than 0, then decrease the balance.\n3) Return: Convert the StringBuilder to a string and return it.\n# Complexity\n- Time complexity:\nO(n) \u2014 Each character is processed once.\n- Space complexity:\nO(n) \u2014 Space for the result string.\n\n# Code\n```java []\nclass Solution {\n    public String removeOuterParentheses(String s) {\n        StringBuilder ans = new StringBuilder();\n        char[] a = s.toCharArray();\n        int n = a.length;\n        int count = 0;\n        for (int i = 1; i < n; i++) {\n            if (a[i] == \'(\') {\n                count++;\n                ans.append(\'(\');\n            } else {\n                if (count == 0) {\n                    i++;\n                } else {\n                    ans.append(\')\');\n                    count--;\n                }\n            }\n        }\n        return ans.toString();\n    }\n}\n\n```\n![cat_new.jpeg](https://assets.leetcode.com/users/images/0dde5182-a643-4fa9-a922-8cd0cd3de98e_1725738868.517184.jpeg)\n
['Java']