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. 洛谷P4559 [JSOI2018]列队 【70分二分 + 主席树】

    题目链接 洛谷P4559 题解 只会做\(70\)分的\(O(nlog^2n)\) 如果本来就在区间内的人是不用动的,区间右边的人往区间最右的那些空位跑,区间左边的人往区间最左的那些空位跑 找到这些空 ...

  2. 【51Nod1386】双马尾机器人Description 解题报告

    [51Nod1386]双马尾机器人Description ​ 给定\(n\)和\(k\),我们要在\(1,2,3,...,n\)中选择若干的数,每一种选择的方案被称为选数方案. ​ 我们定义一种选数方 ...

  3. 升级系统后maxvim不能用,重新下载编译个

    1. 获取macvim源代码git clone https://github.com/b4winckler/macvim.git 2 配置及编译 编译选项 ./configure --with-fea ...

  4. SSH不能连接并提示REMOTE HOST IDENTIFICATION HAS CHANGED解决

    SSH不能连接并提示REMOTE HOST IDENTIFICATION HAS CHANGED解决方法: 如果提示信息如下: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  5. MapReduce(三) 典型场景(一)

    一.mapreduce多job串联 1.需求 一个稍复杂点的处理逻辑往往需要多个 mapreduce 程序串联处理,多 job 的串联可以借助 mapreduce 框架的 JobControl 实现 ...

  6. Ubuntu下Sublime Text 2优化配置

    以前经常用Notepad++,最近因为需要长期在Linux环境下进行C开发,就使用了sublime Text 2,这里就不介绍基本的了主要针对我使用的经验中进行一些总结. 1.pacage contr ...

  7. update condition 字段报错

    mysql> update tf_user_present set condition="0" where id=1;ERROR 1064 (42000): You have ...

  8. 什么是JavaFX

    什么是JavaFX JavaFx平台是一个富客户端平台解决方案,它能够使用应用程序开发人员轻松的创建跨平台的富客户端应用程序.它构建在Java技术的基础之上,JavaFX平台提供了一组丰富的图形和媒体 ...

  9. OpenCV---色彩空间(二)HSV追踪颜色对象和通道分离与合并

    一:HSV追踪有颜色对象 def inRange(src, lowerb, upperb, dst=None) #lowerb是上面每个颜色分段的最小值,upperb是上面每个颜色分段的最大值,都是列 ...

  10. 跟我一起写Makefile(五)

    使用变量———— 在Makefile中的定义的变量,就像是C/C++语言中的宏一样,他代表了一个文本字串,在Makefile中执行的时候其会自动原模原样地展开在所使用的地方.其与C/C++所不同的是, ...