✅ Beats 100 % || C++ || Easy to Understand || Without Using Stack
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to remove the outermost parentheses from each segment of valid parentheses in t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to remove the outermost parentheses from each segment of valid parentheses in the given string. A valid parentheses string is one where every opening parenthesis ( has a matching closing parenthesis ). If we can track how many open and close parentheses we\'ve seen, we can figure out which parentheses are the outermost ones and skip them.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n# **1. Initialize Variables:**\n- \'result\': An empty string to store the final output.\n- balance: A counter to keep track of the balance of parentheses (( increases the balance and ) decreases it).\n\n# **2. Loop Through the String:**\n- Use a for loop to go through each character in the string s.\n- For each character:\n    - If it\u2019s an opening parenthesis (:\n        - If the balance is more than 0, it means this ( is not an outermost parenthesis, so add it to \'result\'.\n        - Increase the balance by 1.\n    - If it\u2019s a closing parenthesis ):\n        - Decrease the balance by 1.\n        - If the balance is more than 0 after decreasing, it means this ) is not an outermost parenthesis, so add it to \'result\'.\n\n# **3. Return the Result:**\n- After processing all characters, the \'result\' string will contain the original string without the outermost parentheses.\n\n# Complexity\n**- Time complexity:**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- The time complexity is O(n), where n is the length of the string s. This is because we go through the string once.\n\n**- Space complexity:**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- The space complexity is O(n), where n is the length of the string s, because the result string might store almost all characters from s.\n\n# Code\n```\nclass Solution {\npublic:\n    // Function to remove outermost parentheses of every primitive string in the\n    // decomposition of s\n    string removeOuterParentheses(string s) {\n        string result;   // To store the final result\n        int balance = 0; // To keep track of the balance of parentheses\n\n        // Iterate through each character in the string\n        for (int i = 0; i < s.size(); i++) {\n            if (s[i] == \'(\') {\n                // If balance is greater than 0, it means this \'(\' is not an\n                // outermost parenthesis\n                if (balance > 0) {\n                    result += s[i]; // Add the character to the result\n                }\n                balance++; // Increase the balance for \'(\'\n            } else {\n                balance--; // Decrease the balance for \')\'\n                // If balance is greater than 0, it means this \')\' is not an\n                // outermost parenthesis\n                if (balance > 0) {\n                    result += s[i]; // Add the character to the result\n                }\n            }\n        }\n\n        return result; // Return the final result after removing outermost\n                       // parentheses\n    }\n};\n```\n\n![Leetcode Upvote.jpeg](https://assets.leetcode.com/users/images/2d66f603-28bd-4a94-89bb-a70503057372_1717482503.2942436.jpeg)\nPLEASE UPVOTE IT IF YOU LIKED IT, IT REALLY MOTIVATES :)\n
['C++']