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. 洛谷 P4027 [NOI2007]货币兑换 解题报告

    P4027 [NOI2007]货币兑换 题目描述 小 \(Y\) 最近在一家金券交易所工作.该金券交易所只发行交易两种金券:\(A\) 纪念券(以下简称 \(A\) 券)和 \(B\) 纪念券(以下简 ...

  2. linux系统启动自动激活网卡的解决方法

    linux每次启动的时候网卡都需要激活才能上网,实在是很麻烦. 上网找了找资料,最后是这样解决的: #   vi   /etc/sysconfig/network-scripts/ifcfg-eth0 ...

  3. 电子商务(电销)平台中用户模块(User)数据库设计明细

    以下是自己在电子商务系统设计中的订单模块的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 用户基础表(user_base)|-- 自动编号 (user_id)|-- 用户名 (us ...

  4. 在Android 下写一个检测软件版本号 以自动升级APP 的插件

    直接上图上代码: 1.插件类的编写 工程目录结构图: 代码如下: package org.apache.cordova.versionupdate; import org.apache.cordova ...

  5. OD脚本指令集

    声明: 1.本指令集搜集自各论坛.博客,欢迎补充讨论 OD脚本指令集 在后面的文档中, “源操作数” 和 “目的操作数”表示以下含义: - 十六进制常数,既没有前缀也没有后缀. (例如:是00FF, ...

  6. POJ 3304 Segments 基础线段交判断

    LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...

  7. php每天自动备份数据库

    php每天自动备份数据库 windows中如何添加计划任务? 前提:添加windows计划任务,每天打开备份数据库的页面.1.打开http://localhost/thinkphp3.2/index. ...

  8. 《JavaScript 实战》:JavaScript 实现拖拽缩放效果

    拖拉缩放效果,实现通过鼠标拖动来调整层的面积(宽高)大小,例如选框效果.这里的拖拉缩放比一般的选框复杂一点,能设置八个方位(方向)的固定触发点,能设置最小范围,最大范围和比例缩放. 跟拖放效果一样,程 ...

  9. 【BZOJ】2120: 数颜色 带修改的莫队算法

    [题意]给定n个数字,m次操作,每次询问区间不同数字的个数,或修改某个位置的数字.n,m<=10^4,ai<=10^6. [算法]带修改的莫队算法 [题解]对于询问(x,y,t),其中t是 ...

  10. iframe子夜页面调父页面的方法 取父页面的值

    1.调父页面的方法的写法 window.parent.yincang();//yincang()是父页面的一个方法 2.取父页面的值的写法 window.parent.document.gettEle ...