minimum remove to make valid parentheses

Solutions on MaxInterview for minimum remove to make valid parentheses by the best coders in the world

showing results for - "minimum remove to make valid parentheses"
Candice
05 Aug 2017
1var minRemoveToMakeValid = function(str) {
2    str = str.split("");
3	let stack = [];
4    for(let i = 0; i<str.length; i++){
5        if(str[i]==="(")
6            stack.push(i);
7        else if(str[i]===")"){
8            if(stack.length) stack.pop();
9            else str[i]="";
10        }
11    }
12    
13    for(let i of stack) str[i] = "";
14    
15    return str.join("");
16	
17}
18