[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:

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

Credits:
Special thanks to @hpplayer for adding this problem and creating all test cases.

Solution:

BFS, 使用validParenthese判断是否合法括号。如果不是合法,则依次去掉第i个括号继续判断。

 1 vector<string> removeInvalidParentheses(string s)
 2     {
 3         vector<string> ret;
 4         unordered_set<string> visit;
 5         queue<string> Q;
 6         Q.push(s);
 7         visit.insert(s);
 8
 9         bool found = false;
10         while (!Q.empty())
11         {
12             string ts = Q.front();
13             Q.pop();
14             if (validParenthese(ts)) // valid parenthese, continue
15             {
16                 ret.push_back(ts);
17                 found = true;
18             }
19             if (found == true)
20                 continue;
21
22             for (int i = 0; i < ts.size(); i++)
23             {
24                 if ((ts[i] != ‘(‘) && (ts[i] != ‘)‘))
25                     continue;
26                 string temp = ts.substr(0, i) + ts.substr(i + 1);
27                 if (visit.find(temp) == visit.end())
28                 {
29                     visit.insert(temp);
30                     Q.push(temp);
31                 }
32             }
33         }
34
35         return ret;
36     }
37
38     bool validParenthese(string s)
39     {
40         int mleft = 0;
41         for (int i = 0; i < s.size(); i++)
42         {
43             if (s[i] == ‘(‘)
44                 mleft++;
45             if (s[i] == ‘)‘)
46                 mleft--;
47             if (mleft < 0)
48                 return false;
49         }
50
51         return (mleft == 0);
52     }
时间: 2024-10-13 22:20:40

[leetcode] 301. Remove Invalid Parentheses的相关文章

[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][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: "()())()"

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】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

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: "()())()" -> ["()()()",

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: [&quo

【leetcode】301. Remove Invalid Parentheses

题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示这个区间是不合法的,需要删掉一个右括号:遍历完成后,如果左括号数量大于右括号的数量,那么需要删除左括号,直至两者相等. 代码如下: class Solution(object): def removeInvalidParentheses(self, s): """ :type s