[LeetCode][JavaScript]Remove Invalid Parentheses

Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note: The input string may contain letters other than the parentheses ( and ).

Examples:

"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]

https://leetcode.com/problems/remove-invalid-parentheses/


( 和 )总是成对出现的,出现在 ( 之前的 ) 没有意义。

开变量countLeft记录还没匹配的 ( 的次数。

每轮循环三种情况:

1. (, 可以选择拿或不拿;

2. ), 如果有没匹配的 ( ,可以选择拿或不拿,否则只能不拿;

3. 其他字母直接拿。

因为要移除最少的括号,开变量maxLeft记录最多有几对括号,即最多出现过几个匹配成功的 (。

注意到这两句话的顺序:

dfs(str.substring(1), subRes + ‘(‘, countLeft + 1, maxLeft + 1);
dfs(str.substring(1), subRes, countLeft, maxLeft);

保证了匹配括号多的结果一定会先出现在结果数组中,不会遗漏结果。

https://leetcode.com/discuss/68272/straight-forward-solution-with-explanation

 1 /**
 2  * @param {string} s
 3  * @return {string[]}
 4  */
 5 var removeInvalidParentheses = function(s) {
 6     var res = [], max = 0;
 7     dfs(s, "", 0, 0);
 8     return res.length !== 0 ? res : [""];
 9
10     function dfs(str, subRes, countLeft, maxLeft){
11         if(str === ""){
12             if(countLeft === 0 && subRes !== ""){
13                 if(maxLeft > max)
14                     max = maxLeft;
15                 if(max === maxLeft && res.indexOf(subRes) === -1)
16                     res.push(subRes);
17             }
18             return;
19         }
20         if(str[0] === ‘(‘){
21             dfs(str.substring(1), subRes + ‘(‘, countLeft + 1, maxLeft + 1);
22             dfs(str.substring(1), subRes, countLeft, maxLeft);
23         }else if(str[0] === ‘)‘){
24             if(countLeft > 0)
25                 dfs(str.substring(1), subRes + ‘)‘, countLeft - 1, maxLeft);
26             dfs(str.substring(1), subRes, countLeft, maxLeft);
27         }else{
28             dfs(str.substring(1), subRes + str[0], countLeft, maxLeft);
29         }
30     }
31 };
时间: 2024-12-24 12:12:45

[LeetCode][JavaScript]Remove Invalid Parentheses的相关文章

【Leetcode】Remove Invalid Parentheses

题目链接:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parent

[leetcode] 301. Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()" -> ["()()()",

[LeetCode] 301. Remove Invalid Parentheses 移除非法括号

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples:"()())()" -> ["()()()", &

[leetcode]301. Remove Invalid Parentheses 去除无效括号

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Example 1: Input: "()())()"Output: ["()()(

LeetCode 301. Remove Invalid Parentheses(DP)

题目 DP 险过. dp[i][j] :means it need remove at least dp[i][j] characters to get vaild parenthese from position i to postion j in string. vector str[i][j] store the parenthese string for example : "()())" dp[0][1]=0 vector[0][1]=["()"] dp[

leetcode Remove Invalid Parentheses

题目连接 https://leetcode.com/problems/remove-invalid-parentheses/ Remove Invalid Parentheses Description Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may co

[LeetCode] Remove Invalid Parentheses 移除非法括号

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()" -> ["()()()",

LeetCode题301—Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Example 1: Input: "()())()" Output: ["()()

301.Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()" -> ["()()()",