LeetCode题301—Remove Invalid Parentheses

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;
    }
}

原文地址:https://www.cnblogs.com/f91og/p/9668209.html

时间: 2024-07-29 21:44:04

LeetCode题301—Remove Invalid Parentheses的相关文章

【leetcode】301. Remove Invalid Parentheses

题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示这个区间是不合法的,需要删掉一个右括号:遍历完成后,如果左括号数量大于右括号的数量,那么需要删除左括号,直至两者相等. 代码如下: class Solution(object): def removeInvalidParentheses(self, s): """ :type s

[leetcode] 301. Remove Invalid Parentheses

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

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

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

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

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

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

[抄题]: 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: [&quo

301.Remove Invalid Parentheses

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

LeetCode 301. Remove Invalid Parentheses(DP)

题目 DP 险过. dp[i][j] :means it need remove at least dp[i][j] characters to get vaild parenthese from position i to postion j in string. vector str[i][j] store the parenthese string for example : "()())" dp[0][1]=0 vector[0][1]=["()"] dp[

leetcode Remove Invalid Parentheses

题目连接 https://leetcode.com/problems/remove-invalid-parentheses/ Remove Invalid Parentheses Description Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may co

[LeetCode][JavaScript]Remove Invalid Parentheses

Remove Invalid Parentheses 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: "()())()"