Skip to content

Commit 9d5b0ef

Browse files
committed
Remove Invalid Parentheses
1 parent 36fa023 commit 9d5b0ef

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

0301_removeInvalidParentheses.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
let validExpressions, minimumRemoved, expression, input, length;
2+
3+
/**
4+
* @param {string} s String containing parentheses ().
5+
* @return {string[]} List of all possible results.
6+
* @summary Remove Invalid Parentheses {@link https://leetcode.com/problems/remove-invalid-parentheses/}
7+
* @description Remove the minimum number of invalid parentheses in order to make the input string valid.
8+
* Space O(n) - on recurrsion maximum call stack depth will be n.
9+
* Time O(2^n) - each step can generate up to two more paths (remoeve or keep parentheses).
10+
*/
11+
const removeInvalidParentheses = s => {
12+
validExpressions = {};
13+
minimumRemoved = Number.MAX_SAFE_INTEGER;
14+
expression = [];
15+
input = s;
16+
length = input.length;
17+
18+
let openRemoveCount = 0;
19+
let closeRemoveCount = 0;
20+
for (let index = 0; index < length; index++) {
21+
if (s[index] === '(') openRemoveCount++;
22+
else if (s[index] === ')') {
23+
if (openRemoveCount === 0) closeRemoveCount++;
24+
if (openRemoveCount > 0) openRemoveCount--;
25+
}
26+
}
27+
28+
removeMinimumParentheses(0, 0, 0, openRemoveCount, closeRemoveCount);
29+
30+
return Object.keys(validExpressions);
31+
};
32+
33+
/**
34+
* @param {number} i Current index.
35+
* @param {number} openCount Number of open parentheses '(' for currently tested solution.
36+
* @param {number} closeCount Number of close parentheses ')' for currently tested solution.
37+
* @param {number} minOpen Remaining count of open parentheses to be removed for optimal solution.
38+
* @param {number} minClose Remaining count of close parentheses to be removed for optimal solution.
39+
* @return {undefined} Actual solution is written to validExpressions.
40+
*/
41+
const removeMinimumParentheses = (i, openCount, closeCount, minOpen, minClose) => {
42+
if (i === length && !minOpen && !minClose) {
43+
const solution = expression.join('');
44+
validExpressions[solution] = true;
45+
} else if (i < length) {
46+
const char = input[i];
47+
48+
if (char === '(' && minOpen > 0) {
49+
removeMinimumParentheses(i + 1, openCount, closeCount, minOpen - 1, minClose);
50+
}
51+
if (char === ')' && minClose > 0) {
52+
removeMinimumParentheses(i + 1, openCount, closeCount, minOpen, minClose - 1);
53+
}
54+
55+
expression.push(char);
56+
57+
if (char !== '(' && char !== ')') {
58+
removeMinimumParentheses(i + 1, openCount, closeCount, minOpen, minClose);
59+
} else if (char === '(') {
60+
removeMinimumParentheses(i + 1, openCount + 1, closeCount, minOpen, minClose);
61+
} else if (closeCount < openCount) {
62+
removeMinimumParentheses(i + 1, openCount, closeCount + 1, minOpen, minClose);
63+
}
64+
65+
expression.pop();
66+
}
67+
};

0 commit comments

Comments
 (0)