Skip to content

Commit 0e3ccb1

Browse files
authored
Create 1249-minimum-remove-to-make-valid-parentheses.js
1 parent 989c1bf commit 0e3ccb1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
const minRemoveToMakeValid = function(s) {
6+
const stack = [], n = s.length
7+
const arr = s.split('')
8+
let res = ''
9+
for(let i = 0; i < n; i++) {
10+
if(s[i] === '(') stack.push(i + 1)
11+
if(s[i] === ')') {
12+
if(stack.length && stack[stack.length - 1] >= 0) stack.pop()
13+
else stack.push(-(i + 1))
14+
}
15+
}
16+
while(stack.length) {
17+
arr[Math.abs(stack.pop()) - 1] = ''
18+
}
19+
return arr.join('')
20+
};

0 commit comments

Comments
 (0)