DEV Community

Discussion on: Solution: Minimum Remove to Make Valid Parentheses

Collapse
 
mrjoyrana profile image
Joy👨🏻‍💻🚀 • Edited

var minRemoveToMakeValid = function(s) {
let words = s.split("");
let open = [];
let closed = [];
for(let i=0;i let c=words[i];
if(c===")") {
closed.push(i);
if(closed.length > open.length) {
closed.pop();
delete words[i];
}

   } else if(c==="(") {
       open.push(i)
   }
Enter fullscreen mode Exit fullscreen mode

}
while(open.length > closed.length) {
let position = open.pop();
delete words[position];
}

return words.join("");
};