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

Example 2:

Input: "(a)())()"
Output: ["(a)()()", "(a())()"]

Example 3:

Input: ")("
Output: [""]

思路:可以利用DFS或者BFS来解这道题,感觉还是BFS简单点,即对于从给定的字符串通过移除 ( 或 ) 来构造所有可能的合法串,如果合法就加入到set集合中,不合法到到下一轮的BFS中。

public class Solution {
public List<String> removeInvalidParentheses(String s) {
List<String> res = new ArrayList<>(); // sanity check
if (s == null) return res; Set<String> visited = new HashSet<>();
Queue<String> queue = new LinkedList<>(); // initialize
queue.add(s);
visited.add(s); boolean found = false; while (!queue.isEmpty()) {
s = queue.poll();

    // 如果当前层次中有合法解的话,只需要将当前层次中的字符串全部弹出判断是否合法,停止BFS,这样保证所得到的合法字符串是移除最少字符得到的
if (isValid(s)) {
// found an answer, add to the result
res.add(s);
found = true;
} if (found) continue; // generate all possible states
for (int i = 0; i < s.length(); i++) {
// we only try to remove left or right paren
if (s.charAt(i) != '(' && s.charAt(i) != ')') continue; String t = s.substring(0, i) + s.substring(i + 1); if (!visited.contains(t)) {
// for each state, if it's not visited, add it to the queue
queue.add(t);
visited.add(t);
}
}
} return res;
} // helper function checks if string s contains valid parantheses
boolean isValid(String s) {
int count = 0; for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') count++;
if (c == ')' && count-- == 0) return false;
} return count == 0;
}
}

LeetCode301. Remove Invalid Parentheses的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)

    Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...

  2. [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses

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

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

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

  4. Remove Invalid Parentheses

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

  5. 301. Remove Invalid Parentheses

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

  6. Remove Invalid Parentheses 解答

    Question Remove the minimum number of invalid parentheses in order to make the input string valid. R ...

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

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

  8. 301. Remove Invalid Parentheses去除不符合匹配规则的括号

    [抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...

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

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

随机推荐

  1. PHP关于VC9和VC6以及Thread Safe和Non Thread Safe版本选择的问题

    一.如何选择PHP5.3的VC9版本和VC6版本 VC6版本是使用Visual Studio 6编译器编译的,如果你的PHP是用Apache来架设的,那你就选择VC6版本.      VC9版本是使用 ...

  2. bzoj3306: 树(dfs序+倍增+线段树)

    比较傻逼的一道题... 显然求子树最小值就是求出dfs序用线段树维护嘛 换根的时候树的形态不会改变,所以我们可以根据相对于根的位置分类讨论. 如果询问的x是根就直接输出整棵树的最小值. 如果询问的x是 ...

  3. LOJ #6035.「雅礼集训 2017 Day4」洗衣服 贪心

    这道题的贪心好迷啊~我们对于两个过程进行单独贪心,然后再翻转一个,把这两个拼起来.先说一下单独贪心,单独贪心的话就是用一个堆,每次取出最小的,并且把这个最小的加上他单次的,再放进去.这样,我们得到的结 ...

  4. python学习(十三)进程和线程

    python多进程 from multiprocessing import Process import os def processFunc(name): print("child pro ...

  5. 题解【bzoj3240 [NOI2013]矩阵游戏】

    挖坑2333 等我把代码写完了再写

  6. logstash 中配置GeoIP解析地理信息

    logstash中配置的GeoIP的数据库解析ip了,这里是用了开源的ip数据源,用来分析客户端的ip归属地.官网在这里:MAXMIND 下载GeoLiteCity数据库 wget http://ge ...

  7. sourceTree的下载与安装

    一. SourceTree是什么? 一个拥有可视化界面的项目版本控制的软件,适用于git项目管理,在window和mac均可使用. 二. SourceTree下载 下载地址:SourceTree官网 ...

  8. Linux下打包压缩war和解压war包 zip和jar

    ============jar================= 把当前目录下的所有文件打包成game.warjar -cvfM0 game.war ./ -c   创建war包-v   显示过程信息 ...

  9. 庞老师集群.ziw

    2017年2月17日, 星期五 庞老师集群 链接:http://pan.baidu.com/s/1mhSw2TE 密码:hzz4   更改子网IP,及网关:     null

  10. 常用的20个强大的 Sublime Text 插件

    作为一个开发者你不可能没听说过 Sublime Text.不过你没听说过也没关系,下面让你明白. Sublime Text是一款非常精巧的文本编辑器,适合编写代码.做笔记.写文章.它用户界面十分整洁, ...