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