Description
Submission
class Solution { public: string minRemoveToMakeValid(string s) { int left = 0, right = 0; string tmp; for(char ch: s) { if(ch == '(') { ++left; tmp.push_back(ch); } else if(ch == ')') { if(!left) ++right; else { --left; tmp.push_back(ch); } } else { tmp.push_back(ch); } } string ret; for(int i = tmp.length() - 1; i >= 0; --i) { if(tmp[i] == '(' && left > 0) { --left; } else { ret.push_back(tmp[i]); } } reverse(ret.begin(), ret.end()); return ret; } };