C++ inplace removal, O(n) time, O(1) space
\n    string minRemoveToMakeValid(string s)\n    {\n        int i = s.size();\n        int balance = 0;\n        for (int j = s.size() - 1; j >= 0; j--) {\n    
```\n    string minRemoveToMakeValid(string s)\n    {\n        int i = s.size();\n        int balance = 0;\n        for (int j = s.size() - 1; j >= 0; j--) {\n            if (s[j] == \')\') balance++;\n            else if (s[j] == \'(\') {\n                if (balance == 0) continue;\n                balance--;\n            }\n            s[--i] = s[j];\n        }\n        int len = 0;\n        balance = 0;\n        for (; i < s.size(); i++) {\n            if (s[i] == \'(\') balance++;\n            else if (s[i] == \')\') {\n                if (balance == 0) continue;\n                balance--;\n            }\n            s[len++] = s[i];\n        }\n        s.erase(len);\n        return s;\n    }\n```
[]