|
8 | 8 | import java.util.Set;
|
9 | 9 |
|
10 | 10 | /**
|
| 11 | + * 301. Remove Invalid Parentheses |
| 12 | + * |
11 | 13 | * Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
|
12 | 14 |
|
13 | 15 | Note: The input string may contain letters other than the parentheses ( and ).
|
|
19 | 21 | */
|
20 | 22 | public class _301 {
|
21 | 23 |
|
22 |
| - public List<String> removeInvalidParentheses(String s) { |
23 |
| - List<String> result = new ArrayList<>(); |
24 |
| - if (s == null) { |
25 |
| - return result; |
26 |
| - } |
| 24 | + public static class Solution1 { |
27 | 25 |
|
28 |
| - Set<String> visited = new HashSet(); |
29 |
| - Queue<String> q = new LinkedList(); |
| 26 | + public List<String> removeInvalidParentheses(String s) { |
| 27 | + List<String> result = new ArrayList<>(); |
| 28 | + if (s == null) { |
| 29 | + return result; |
| 30 | + } |
30 | 31 |
|
31 |
| - q.offer(s); |
32 |
| - visited.add(s); |
| 32 | + Set<String> visited = new HashSet(); |
| 33 | + Queue<String> q = new LinkedList(); |
33 | 34 |
|
34 |
| - boolean found = false; |
| 35 | + q.offer(s); |
| 36 | + visited.add(s); |
35 | 37 |
|
36 |
| - while (!q.isEmpty()) { |
37 |
| - String curr = q.poll(); |
38 |
| - if (isValid(curr)) { |
39 |
| - found = true; |
40 |
| - result.add(curr); |
41 |
| - } |
| 38 | + boolean found = false; |
42 | 39 |
|
43 |
| - if (found) { |
44 |
| - continue;//this means if the initial input is already a valid one, we'll just directly return it and there's actually only one valid result |
45 |
| - } |
| 40 | + while (!q.isEmpty()) { |
| 41 | + String curr = q.poll(); |
| 42 | + if (isValid(curr)) { |
| 43 | + found = true; |
| 44 | + result.add(curr); |
| 45 | + } |
46 | 46 |
|
47 |
| - for (int i = 0; i < curr.length(); i++) { |
48 |
| - if (curr.charAt(i) != '(' && curr.charAt(i) != ')') { |
49 |
| - continue;//this is to rule out those non-parentheses characters |
| 47 | + if (found) { |
| 48 | + continue;//this means if the initial input is already a valid one, we'll just directly return it and there's actually only one valid result |
50 | 49 | }
|
51 | 50 |
|
52 |
| - String next = curr.substring(0, i) + curr.substring(i + 1); |
53 |
| - if (!visited.contains(next)) { |
54 |
| - q.offer(next); |
55 |
| - visited.add(next); |
| 51 | + for (int i = 0; i < curr.length(); i++) { |
| 52 | + if (curr.charAt(i) != '(' && curr.charAt(i) != ')') { |
| 53 | + continue;//this is to rule out those non-parentheses characters |
| 54 | + } |
| 55 | + |
| 56 | + String next = curr.substring(0, i) + curr.substring(i + 1); |
| 57 | + if (!visited.contains(next)) { |
| 58 | + q.offer(next); |
| 59 | + visited.add(next); |
| 60 | + } |
56 | 61 | }
|
57 |
| - } |
58 | 62 |
|
| 63 | + } |
| 64 | + return result; |
59 | 65 | }
|
60 |
| - return result; |
61 |
| - } |
62 | 66 |
|
63 |
| - private boolean isValid(String str) { |
64 |
| - char[] chars = str.toCharArray(); |
65 |
| - int count = 0; |
66 |
| - for (int i = 0; i < chars.length; i++) { |
67 |
| - char c = chars[i]; |
68 |
| - if (c == '(') { |
69 |
| - count++; |
70 |
| - } |
71 |
| - if (c == ')') { |
72 |
| - count--; |
73 |
| - if (count == -1) { |
74 |
| - return false; |
| 67 | + private boolean isValid(String str) { |
| 68 | + char[] chars = str.toCharArray(); |
| 69 | + int count = 0; |
| 70 | + for (int i = 0; i < chars.length; i++) { |
| 71 | + char c = chars[i]; |
| 72 | + if (c == '(') { |
| 73 | + count++; |
| 74 | + } |
| 75 | + if (c == ')') { |
| 76 | + count--; |
| 77 | + if (count == -1) { |
| 78 | + return false; |
| 79 | + } |
75 | 80 | }
|
76 | 81 | }
|
| 82 | + return count == 0; |
77 | 83 | }
|
78 |
| - return count == 0; |
79 | 84 | }
|
80 | 85 | }
|
0 commit comments