zoukankan      html  css  js  c++  java
  • 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: [""]
    class Solution {
        public List<String> removeInvalidParentheses(String s) {
            List<String> res = new ArrayList();
            if(s == null) return res;
            boolean inthislevel = false;
            Set<String> set = new HashSet<>();
            Queue<String> q = new LinkedList();
            q.add(s);
            set.add(s);
    
            while(!q.isEmpty()) {
                String cur = q.poll();
                if(isValid(cur)) {
                    inthislevel = true;
                    res.add(cur);
                }
                if(inthislevel) continue;
                
                for(int i = 0; i < cur.length(); i++) {
                    if(cur.charAt(i) != '(' && cur.charAt(i) != ')') continue;
                    String t = cur.substring(0, i) + cur.substring(i+1, cur.length());
                    if(!set.contains(t)) {
                        set.add(t);
                        q.add(t);
                    }
                }
            }
            return res;
        }
        public boolean isValid(String s) {
            int count = 0;
            for(char c: s.toCharArray()) {
                if(c == '(') count++;
                else if(c == ')'){
                    count--;
                    if(count < 0) return false;
                }
            }
            return count == 0;
        }
    }

    这题的逻辑还挺简单的(看了答案后 https://leetcode.com/problems/remove-invalid-parentheses/discuss/75032/Share-my-Java-BFS-solution

    总结:

    This question asked us to return the valid string that made by remove minimum parenthesis.

    So we can think like this, we start from removing one parenthesis and check if there's valid answer, if all remove-one string are not valid, we go to the next stage which is remove two ... and so On

    One thing need to pay attention is once we found a valid string in current level, we just use a boolean flag and make it true, then we just need to go through strings in current level instead going deeper.

    Then to realize this, we use a queue to store current level's strings, a HashSet to store visited mid-point strings to avoid repeat answers.

    Then a boolean variable inthislevel which means we found valid answer in this level and don't need to go deeper

    In every level we tried to remove one parenthesis and add it to set and queue.

    Another thing is when we encounter non parenthesis chars ,we just skip.

  • 相关阅读:
    算法 字符串最后一个单词的长度【水】
    算法 按照ASII码从小到大输出字符(数量最多)
    算法 计算一个数转换为二进制后有多少个1
    算法 数字颠倒
    算法 浮点数取整
    算法 进制转换
    C++ 浅析移位运算
    C++ 浅析调试,内存重叠查看
    DB2 close auto commit
    【DB2】Event monitor for locking
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13335233.html
Copyright © 2011-2022 走看看