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)
}
}
while(open.length > closed.length) {
let position = open.pop();
delete words[position];
}
return words.join("");
};
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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];
}
}
while(open.length > closed.length) {
let position = open.pop();
delete words[position];
}
return words.join("");
};