Two approaches - Stack & Without Stack O(1) space
1. Solution with Stack\nAdd ( to stack index corresponding to the current length of res and skip adding of ) if the stack is empty\n\n\nvar minRemoveToMakeValid
**1. Solution with Stack**\nAdd `(` to stack index corresponding to the current length of `res` and skip adding of `)` if the stack is empty\n\n```\nvar minRemoveToMakeValid = function(s) {\n    let res = []\n    let stack = []\n\t\n    for(let i=0; i<s.length; i++){\n        if(s[i] == \'(\') stack.push(res.length)\n        else if(s[i] == \')\'){\n            if(stack.length) stack.pop()\n            else continue\n        }\n        res.push(s[i])\n    }\n    \n    while(stack.length){\n        res[stack.pop()] = \'\'\n    }\n\n    return res.join(\'\')\n};\n```\n\n**2. Solution without Stack**\nThis approach is O(1) space without considering the ouptput array\n\n```\nvar minRemoveToMakeValid = function(s) {\n    let res = []\n    let r = 0\n    let open = 0\n    \n    for(let char of s){\n        if(char == \')\') r++\n    }\n    \n    for(let i=0; i<s.length; i++){\n        if(s[i] == \')\'){\n            if(open > 0){\n                res.push(s[i])\n                open--\n            }\n            else r--\n        } else if(s[i] == \'(\'){\n            if(r>0){\n                res.push(s[i])\n                open++\n                r--\n            }\n        } else res.push(s[i])\n    }\n    \n    return res.join(\'\')\n};\n```
['Stack', 'JavaScript']