Solution: iteration + stack operation

The first step is to convert the string into an array for easy operation. Step 2: Iterate through the number group, if encountered “(“, add index to the stack, if encountered”) “, need to judge. If the length of the stack is 0, it is null and is directly empty (convenient for later join deletion). If there is a value in the stack, the stack is pushed out. Step 3: If the stack length is not zero after the loop, there is extra “(“, iterate through the stack and empty the index element from the array. Step 4: Return the string after join(“)

Var minRemoveToMakeValid = function(s) {var minRemoveToMakeValid = function(s) {var minRemoveToMakeValid = function(s) {var minRemoveToMakeValid = function(s) {var minRemoveToMakeValid = function(s) {var minRemoveToMakeValid = function(s) { i < arr.length; i++) { if (arr[i] === '(') { stack.push(i) } if (arr[i] === ')') { if (stack.length <= 0) { arr[i] = '' } else { Stack.pop ()}}} for (let I = 0; i < stack.length; I++) {arr [stack [I]] = '} / / step 4 return arr. Join (' ')};Copy the code