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

这个题目的思路参考地址,是用Java写的, 不得不说, 太牛逼了.

主要思路我理解为, 利用Count去看一个valid parentheses, 然后一旦发现不是valid的时候, 那么我们就在目前的部分string里面, 分别将 ')' 去掉一个, 但是这样会出现的问题是我们可能有重复的情况, 比如说s = ()), we can remove s[1] or s[2] but the result is the same (). Thus, we restrict ourself to remove the first ) in a series of concecutive )s.

那么去掉多余的')' 之后, prefix 变成valid, 然后再recursively call 剩下的string, 但是我们只需要再从上次remove掉的那个位置开始检测即可,否则的话又有重复的. For this, we keep tracking the last removal position and only remove ‘)’ after that.

以上的做法就是去掉多余的')', 比如"()()())", 但是没有办法解决"()()("这样的情况, 那么思路类似,就是将以上的做法, 从右向左走一遍, 然后count计数的时候调换下顺序即可, 但是这个思路牛逼的地方就是,它将s reverse, 然后还是用原来的code, 只是加多一个parameter pairs即可. 实在是太牛逼了. 如果还是看不懂的话建议去看上面参考地址的解释, 很详细, 都是英文, 但是解释的肯定比我好, 比我清楚. 不得不再说一次, 真的太腻害了!

Update from 04/10/2020:

必须有prej,不能只有prei,否则的话input为“()()(())))()”, 因为会错过在有‘)’之前的多余的选择。

有prej:

['(((())))()', '(()(()))()', '()((()))()', '()()(())()']

无prej:

['(()(()))()', '()((()))()', '()()(())()']

1. Constraints

1) s可以为empty

2) 每个元素可以是除了"()"之外的元素

2. Idea

利用所谓的recursive helper function 去帮忙, 不知道时间复杂度是多少.

3.Code:

 class Solution:
def removeInvalidParentheses(self, s):
def helper(s, ans, prei, prej, pairs):
count = 0
for i in range(prei, len(s)):
if s[i] == pairs[0]: count += 1
if s[i] == pairs[1]: count -= 1
if count >= 0: continue
for j in range(prej, i+1): # note it is i+1, because until i
if s[j] == pairs[1] and (j == prej or s[j-1] != s[j]):
helper(s[:j] + s[j+1:], ans, i, j, pairs) # should be i + 1, and j + 1, but delete s[j], so i + 1 - 1, j + 1 - 1, in the end i and j
return # the reason it has a return here is finish the helper if it gets into the loop j, otherwise will keep looping for i even the counter is -1.
revs = s[::-1]
if pairs[0] == '(':
helper(revs, ans, 0, 0, ")(")
else:
ans.append(revs)
18     ans = []
19     helper(s, ans, 0, 0, "()")
20     return ans

4. Test cases

1) ""

2) "()"

3) "())"

4) "(()"

5)

"()())()"

[LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS的更多相关文章

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

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

  2. LeetCode 301. Remove Invalid Parentheses

    原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...

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

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

  4. 【leetcode】301. Remove Invalid Parentheses

    题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...

  5. 301. Remove Invalid Parentheses

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

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

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

  7. [LeetCode] 310. Minimum Height Trees_Medium tag: BFS

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  8. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  9. [LeetCode] 101. Symmetric Tree_ Easy tag: BFS

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

随机推荐

  1. java基础---->git的使用(一)

    这里面记录一下git的使用,只是平时工作中遇到的一些问题的解决方案,不会涉及到git的一些基础概念及说明.人的天性便是这般凉薄,只要拿更好的来换,一定舍得. Git的一些使用 一.在码云建立好仓库之后 ...

  2. smarty assign变量赋值

    1.变量赋值的两种写法 <%assign var="name" value="cl"%> <%assign "name" ...

  3. Android 1.6 PackageParser.java 源码分析

    文件目录: android-1.6_r2\frameworks\base\core\java\android\content\pm\PackageParser.java # PackageParser ...

  4. 九度OJ小结

    1. 高精度问题 可参考题目 题目1137:浮点数加法   http://ac.jobdu.com/problem.php?pid=1137 对于高精度问题可以考虑使用结构体.上述为浮点数加法,因此该 ...

  5. python selenium中等待元素出现及等待元素消失操作

    在自动化测试中,很多时候都会有等待页面某个元素出现后能进行下一步操作,或者列表中显示加载,直到加载完成后才进行下一步操作,但时间都不确定,如下图所示 幸运的是,在selenium 2后有一个模块exp ...

  6. mui+vue+photoclip做APP头像裁剪上传

    做APP由于项目需要,需要做用户头像上传的功能,头像上传包括拍照和相册选择图片进行上传,这里使用的技术是mui封装的plus,在进行图片裁剪的时候,使用的是photoclip来进行裁剪,由于个人在使用 ...

  7. 生成式对抗网络GAN 的研究进展与展望

    生成式对抗网络GAN的研究进展与展望.pdf 摘要: 生成式对抗网络GAN (Generative adversarial networks) 目前已经成为人工智能学界一个热门的研究方向. GAN的基 ...

  8. linux如何给程序添加自启动

    我要使我的服务程序在重启系统后也随之自动启动.启动我的服务用到了一个脚本.现在有两个方法: 法1: sudo vi /etc/init.d/rc.local在这里添加启动服务的脚本命令. 这个方法的优 ...

  9. slam学习资源

    从零开始学slam: http://blog.csdn.net/akunainiannian/article/details/45363731 史上最全的SLAM学习资料收集  http://www. ...

  10. Oracle Function: NVL

    Description The Oracle/PLSQL NVL function lets you substitute a value when a null value is encounter ...