-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0301-remove-invalid-parentheses.js
48 lines (43 loc) · 1.32 KB
/
0301-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
38
39
40
41
42
43
44
45
46
47
48
/**
* 301. Remove Invalid Parentheses
* https://leetcode.com/problems/remove-invalid-parentheses/
* Difficulty: Hard
*
* Given a string s that contains parentheses and letters, remove the minimum number of
* invalid parentheses to make the input string valid.
*
* Return a list of unique strings that are valid with the minimum number of removals.
* You may return the answer in any order.
*/
/**
* @param {string} s
* @return {string[]}
*/
var removeInvalidParentheses = function(s) {
const result = new Set();
let minRemoved = Infinity;
backtrack(s, 0, 0, 0, 0, '');
return Array.from(result);
function backtrack(str, index, open, close, removed, curr) {
if (index === s.length) {
if (open === close && removed <= minRemoved) {
if (removed < minRemoved) {
result.clear();
minRemoved = removed;
}
result.add(curr);
}
return;
}
if (s[index] !== '(' && s[index] !== ')') {
backtrack(str, index + 1, open, close, removed, curr + s[index]);
} else {
backtrack(str, index + 1, open, close, removed + 1, curr);
if (s[index] === '(') {
backtrack(str, index + 1, open + 1, close, removed, curr + '(');
} else if (close < open) {
backtrack(str, index + 1, open, close + 1, removed, curr + ')');
}
}
}
};