forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove Invalid Parentheses.js
37 lines (35 loc) · 1 KB
/
Remove Invalid Parentheses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Runtime: 161 ms (Top 67.20%) | Memory: 49.7 MB (Top 36.80%)
var removeInvalidParentheses = function(s) {
const isValid = (s) => {
const stack = [];
for(let c of s) {
if(c == '(') stack.push(c);
else if(c == ')') {
if(stack.length && stack.at(-1) == '(')
stack.pop();
else return false;
}
}
return stack.length == 0;
}
const Q = [s], vis = new Set([s]), ans = [];
let found = false;
while(Q.length) {
const top = Q.shift();
if(isValid(top)) {
ans.push(top);
found = true;
}
if(found) continue;
for(let i = 0; i < top.length; i++) {
if(!'()'.includes(top[i])) continue;
const str = top.slice(0, i) + top.slice(i + 1);
if(!vis.has(str)) {
Q.push(str);
vis.add(str);
}
}
}
if(ans.length == 0) return [''];
return ans;
};