✅Easy✨||C++|| Beats 100% || With Explanation ||
Intuition\n Describe your first thoughts on how to solve this problem. \nWe remove unnecessary parentheses by iterating through the string twice: first loop to 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe remove unnecessary parentheses by iterating through the string twice: first loop to remove excess closing parentheses,second loop to remove extra opening parentheses.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize an empty string temp, which will store the processed string without unnecessary closing parentheses, and a counter open for tracking open parentheses.\n2. Iterate through each character in the input string s.\n. If the character is \'(\', add it to temp and increment open.\n. If the character is \')\' and there are open parentheses (i.e., open > 0), add it to temp and decrement open.\n. If the character is neither \'(\' nor \')\', add it directly to temp.\n3. After the first pass, temp contains a string with balanced parentheses and no extra closing parentheses.\n4. Iterate through temp in reverse order (so that the opening parenthesis without having corresponding closing parenthesis come first ):\n. If an opening parentheses \'(\' is encountered and there are still unmatched open parentheses (open > 0), decrement open.\n. Otherwise, add the character to the final answer string ans.\n5. Reverse ans and return it as the result.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n# Code\n```\nclass Solution {\npublic:\n    string minRemoveToMakeValid(string s) {\n        ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);\n\n        string temp="",ans="";\n        int open=0;\n        for(int i=0;i<s.size();i++){\n            if(s[i]==\'(\')temp+=s[i],open++;\n            else if(s[i]==\')\' && open>0)temp+=s[i],open--;\n            else if(s[i]!=\'(\' && s[i]!=\')\')temp+=s[i];\n        }\n        for(int i=temp.size()-1;i>=0;i--){\n            if(temp[i]==\'(\' && open>0)open--;\n            else ans+=temp[i];\n        }\n        reverse(ans.begin(),ans.end());\n        return ans;    \n    }\n};\n```
['C++']