Without STACK .. Beginner Friendly Solution ..Beats no one..
Intuition\n Describe your first thoughts on how to solve this problem. \nAs there are only one type of brackets we don\'t need any stack to balance them .. we c
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs there are only one type of brackets we don\'t need any stack to balance them .. we can just maintain a count variable .. For eg if cnt==3 it means we have encountered 3 more opening brackets than closing brackets..something like this .. "()()(()(("\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSo firstly we will try to balance the closing brackets.. Balancing them means removing those \')\' which come before \'(\' .. we can do this just maintain a cnt variable and increment and decrement it when we encounter opening and closing bracket respectively .. \n\nAfter removing those extra closing brackets we store the string in new string temp .. Rest of the string remains same .. \nFor eg -> "))()()(" gets changed to "()()(" (we have removed first two closing brackets)\n\nnow we check how many extra opening brackets we have .. after the first step we are sure that no extra closing brackets only we can have extra opening brackets .. \n\nAnd to remove them we should not remove them from beginning as they would imbalance the closing brackets .. we should remove from the last to balance them ..\n\nTo do this .. just count how many extra opening brackets we have and just remove them from last .. and at last reverse the string and return \n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n    string minRemoveToMakeValid(string s) {\n        int cnt=0;\n    \n        string temp="";\n        // temp string stores the string after removing extra closing brackets \n        for(auto ch:s){\n            if(ch==\')\'&&cnt==0){\n                cnt=0;\n                continue;\n\n            }\n            if(ch==\'(\') cnt++;\n            if(ch==\')\') cnt--;\n            temp+=ch;\n        }\n        \n        cnt=0;\n        // now cnt stores the extra opening brackets..\n        for(auto c:temp){\n            if(c==\'(\') cnt++;\n            if(c==\')\') cnt--;\n        }\n        // if 0 it is balanced already \n        if(cnt==0) return temp;\n        string rev="";\n      \n        // traverse from end and remove cnt number of opening brackets\n        for(int i=temp.length()-1;i>=0;i--){\n            if(temp[i]==\'(\'&&cnt>0){\n                cnt--;\n                continue;\n            }\n            rev+=temp[i];\n\n\n        }\n        // reverse it and return \n\n        reverse(rev.begin(),rev.end());\n        \n        return rev;\n        \n    }\n};\n```
['C++']