-
Notifications
You must be signed in to change notification settings - Fork 1
/
0301_removeInvalidParentheses.js
67 lines (58 loc) · 2.52 KB
/
0301_removeInvalidParentheses.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
let validExpressions, minimumRemoved, expression, input, length;
/**
* @param {string} s String containing parentheses ().
* @return {string[]} List of all possible results.
* @summary Remove Invalid Parentheses {@link https://leetcode.com/problems/remove-invalid-parentheses/}
* @description Remove the minimum number of invalid parentheses in order to make the input string valid.
* Space O(n) - on recurrsion maximum call stack depth will be n.
* Time O(2^n) - each step can generate up to two more paths (remoeve or keep parentheses).
*/
const removeInvalidParentheses = s => {
validExpressions = {};
minimumRemoved = Number.MAX_SAFE_INTEGER;
expression = [];
input = s;
length = input.length;
let openRemoveCount = 0;
let closeRemoveCount = 0;
for (let index = 0; index < length; index++) {
if (s[index] === '(') openRemoveCount++;
else if (s[index] === ')') {
if (openRemoveCount === 0) closeRemoveCount++;
if (openRemoveCount > 0) openRemoveCount--;
}
}
removeMinimumParentheses(0, 0, 0, openRemoveCount, closeRemoveCount);
return Object.keys(validExpressions);
};
/**
* @param {number} i Current index.
* @param {number} openCount Number of open parentheses '(' for currently tested solution.
* @param {number} closeCount Number of close parentheses ')' for currently tested solution.
* @param {number} minOpen Remaining count of open parentheses to be removed for optimal solution.
* @param {number} minClose Remaining count of close parentheses to be removed for optimal solution.
* @return {undefined} Actual solution is written to validExpressions.
*/
const removeMinimumParentheses = (i, openCount, closeCount, minOpen, minClose) => {
if (i === length && !minOpen && !minClose) {
const solution = expression.join('');
validExpressions[solution] = true;
} else if (i < length) {
const char = input[i];
if (char === '(' && minOpen > 0) {
removeMinimumParentheses(i + 1, openCount, closeCount, minOpen - 1, minClose);
}
if (char === ')' && minClose > 0) {
removeMinimumParentheses(i + 1, openCount, closeCount, minOpen, minClose - 1);
}
expression.push(char);
if (char !== '(' && char !== ')') {
removeMinimumParentheses(i + 1, openCount, closeCount, minOpen, minClose);
} else if (char === '(') {
removeMinimumParentheses(i + 1, openCount + 1, closeCount, minOpen, minClose);
} else if (closeCount < openCount) {
removeMinimumParentheses(i + 1, openCount, closeCount + 1, minOpen, minClose);
}
expression.pop();
}
};