✅ 2 POINTER APPROACH || Beats 100%✅ || C++ || Without Using Stack || BEGINNER FRIENDLY
Intuition\nGiven the problem of removing the outermost parentheses from every primitive valid parentheses string in a given valid parentheses string s, the main
# Intuition\nGiven the problem of removing the outermost parentheses from every primitive valid parentheses string in a given valid parentheses string `s`, the main idea is to identify these primitive segments and remove their outermost parentheses. A two-pointer approach can be effectively used to identify these segments.\n\n# Approach\n1. Use two pointers, `left` and `right`, to traverse the string `s`.\n2. Maintain a counter `cnt` to keep track of the balance of parentheses.\n3. Increment `right` and update `cnt` based on whether the current character is `(` or `)`.\n4. When `cnt` reaches zero, it means we have a complete primitive valid parentheses string from `left` to `right`.\n5. Extract the inner content of this primitive string by taking the substring from `left + 1` to `right - 1` and append it to the result.\n6. Reset `left` to `right + 1` and continue until the end of the string is reached.\n\n# Complexity\n- Time complexity:\n$$O(n)$$, where \\(n\\) is the length of the string. Each character is processed once.\n\n- Space complexity:\n$$O(n)$$, where \\(n\\) is the length of the string. The resulting string `ans` is at most the size of the input string `s`.\n\n# Code\n```cpp\nclass Solution {\npublic:\n    string removeOuterParentheses(string s) {\n        int left = 0, right = 0;\n        string ans = "";\n        int n = s.length(), cnt = 0;\n        \n        while (right < n) {\n            if (s[right] == \'(\') {\n                cnt++;\n            } else {\n                cnt--;\n            }\n            \n            if (cnt == 0) {\n                if (left <= right) {\n                    ans += s.substr(left + 1, right - left - 1);\n                    right++;\n                    left = right;\n                    continue;\n                }\n            }\n            right++;\n        }\n        \n        return ans;\n    }\n};\n```\n\n![image.png](https://assets.leetcode.com/users/images/9b38dbc2-f1a5-4b57-85b9-6728d292d5d9_1718034205.5414898.png)\n\n\nPLEASE UPVOTE IT IF YOU LIKED IT, IT REALLY MOTIVATES ME A LOT:)
['Two Pointers', 'String', 'C++']