Skip to content

Commit fcb3a02

Browse files
authored
Update 1249-minimum-remove-to-make-valid-parentheses.js
1 parent 38723ab commit fcb3a02

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1249-minimum-remove-to-make-valid-parentheses.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,27 @@ const minRemoveToMakeValid = function(s) {
4949
}
5050
return res.join('')
5151
};
52+
53+
// another
54+
55+
/**
56+
* @param {string} s
57+
* @return {string}
58+
*/
59+
const minRemoveToMakeValid = function(s) {
60+
const stk = [], arr = s.split(''), n = s.length
61+
for(let i = 0; i < n; i++) {
62+
if(s[i] === '(') stk.push(i)
63+
if(s[i] === ')') {
64+
if(stk.length && stk[stk.length - 1] >= 0) stk.pop()
65+
else stk.push(-(i + 1))
66+
}
67+
}
68+
69+
while(stk.length) {
70+
const tmp = stk.pop()
71+
if(tmp < 0) arr[-tmp - 1] = ''
72+
else arr[tmp] = ''
73+
}
74+
return arr.join('')
75+
};

0 commit comments

Comments
 (0)