leetcode:bfs: Remove Invalid Parentheses(301)

原创 2016年08月31日 15:22:07

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())()”]
“)(” -> [“”]


class Solution {
public:
    vector<string> removeInvalidParentheses(string s) {
        vector<string> res;
        unordered_map<string, int> visited;
        queue<string> q;
        q.push(s);
        ++visited[s];
        bool found = false;
        while (!q.empty()) {
            s = q.front(); q.pop();
            if (isValid(s)) {
                res.push_back(s);
                found = true;
            }
            if (found) continue;
            for (int i = 0; i < s.size(); ++i) {
                if (s[i] != '(' && s[i] != ')') continue;
                string t = s.substr(0, i) + s.substr(i + 1);
                if (visited.find(t) == visited.end()) {
                    q.push(t);
                    ++visited[t];
                }
            }
        }
        return res;
    }
    bool isValid(string t) {
        int cnt = 0;
        for (int i = 0; i < t.size(); ++i) {
            if (t[i] == '(') ++cnt;
            if (t[i] == ')' && cnt-- == 0) return false;
        }
        return cnt == 0;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode 301 Remove Invalid Parentheses (BFS + 剪枝)

LeetCode 301 Remove Invalid Parentheses (BFS + 剪枝)

Leetcode 301. Remove Invalid Parentheses

301. Remove Invalid Parentheses Total Accepted: 21411 Total Submissions: 63217 Difficulty: Hard ...

LeetCode 301. Remove Invalid Parentheses(删除无效的括号)

原题网址:https://leetcode.com/problems/remove-invalid-parentheses/ Remove the minimum number of invali...
  • jmspan
  • jmspan
  • 2016年04月19日 04:44
  • 1177

leetcode 301. Remove Invalid Parentheses

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

【LeetCode】301. Remove Invalid Parentheses

题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Retur...

[leetcode] 301. Remove Invalid Parentheses

题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Retur...

【Leetcode】301. Remove Invalid Parentheses 移除非法小括号

题目https://leetcode.com/problems/remove-invalid-parentheses/?tab=Solutions。 题意是给出一个string,其中的小括号可能不配对...

leetcode -- Remove Invalid Parentheses--又是DFS,BFS的题目

https://leetcode.com/problems/remove-invalid-parentheses/valid parentheses definition字符串中的左右括号数应该相同,...
  • xyqzki
  • xyqzki
  • 2015年12月25日 17:00
  • 3045

301. Remove Invalid Parentheses 去掉不合理的括号

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

301. Remove Invalid Parentheses (Hard)

原题目:    Remove the minimum number of invalid parentheses in order to make the input string valid. R...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:bfs: Remove Invalid Parentheses(301)
举报原因:
原因补充:

(最多只允许输入30个字)