Remove Invalid Parentheses 解答

Question

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:

"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]

Solution

看到parenthese的问题,第一反应是用栈。这题要求minimum number,所以想到用BFS遍历解空间树。

思路为:

层次依次为删除0个元素,1个元素,2个元素。。。

层次遍历所有的可能。如果有一种可能是valid,那么不再遍历下面的层。

 1 public class Solution {
 2     public List<String> removeInvalidParentheses(String s) {
 3         Set<String> visited = new HashSet<String>();
 4         List<String> result = new ArrayList<String>();
 5         List<String> current = new ArrayList<String>();
 6         List<String> next;
 7         current.add(s);
 8         boolean reached = false;
 9         // BFS
10         while (!current.isEmpty()) {
11             next = new ArrayList<String>();
12             for (String prev : current) {
13                 visited.add(prev);
14                 // If valid
15                 if (isValid(prev)) {
16                     reached = true;
17                     result.add(prev);
18                 }
19
20                 // If not reached, then delete
21                 if (!reached) {
22                     for (int i = 0; i < prev.length(); i++) {
23                         char tmp = prev.charAt(i);
24                         if (tmp != ‘(‘ && tmp != ‘)‘) {
25                             continue;
26                         }
27                         String newStr = prev.substring(0, i) + prev.substring(i + 1);
28                         if (!visited.contains(newStr)) {
29                             next.add(newStr);
30                             visited.add(newStr);
31                         }
32                     }
33                 }
34             }
35             if (reached) {
36                 break;
37             }
38             current = next;
39         }
40         return result;
41     }
42
43     private boolean isValid(String s) {
44         Deque<Integer> stack = new LinkedList<Integer>();
45         for (int i = 0; i < s.length(); i++) {
46             char cur = s.charAt(i);
47             if (cur != ‘(‘ && cur != ‘)‘) {
48                 continue;
49             }
50             if (cur == ‘(‘) {
51                 stack.push(i);
52             }
53             if (cur == ‘)‘) {
54                 if (stack.isEmpty()) {
55                     return false;
56                 } else {
57                     stack.pop();
58                 }
59             }
60         }
61         return stack.isEmpty();
62     }
63 }

事实上,我们可以不用栈,用一个count来检测是否是valid parenthese.

 1 private boolean isValid(String s) {
 2     int count = 0;
 3     for (int i = 0; i < s.length(); i++) {
 4         char cur = s.charAt(i);
 5         if (cur != ‘(‘ && cur != ‘)‘) {
 6             continue;
 7         }
 8         if (cur == ‘(‘) {
 9             count++;
10         }
11         if (cur == ‘)‘) {
12             count--;
13             if (count < 0) {
14                 return false;
15             }
16         }
17     }
18     return count == 0;
19 }
时间: 2024-11-10 01:29:47

Remove Invalid Parentheses 解答的相关文章

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

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】Remove Invalid Parentheses

题目链接:https://leetcode.com/problems/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 parent

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

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