Skip to content

Commit 4a62a1d

Browse files
refactor 301
1 parent 64449d8 commit 4a62a1d

File tree

1 file changed

+51
-63
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+51
-63
lines changed

src/main/java/com/fishercoder/solutions/_301.java

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,67 @@
77
import java.util.Queue;
88
import java.util.Set;
99

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-
*/
2210
public class _301 {
2311

24-
public static class Solution1 {
12+
public static class Solution1 {
2513

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+
}
3119

32-
Set<String> visited = new HashSet();
33-
Queue<String> q = new LinkedList();
20+
Set<String> visited = new HashSet();
21+
Queue<String> q = new LinkedList();
3422

35-
q.offer(s);
36-
visited.add(s);
23+
q.offer(s);
24+
visited.add(s);
3725

38-
boolean found = false;
26+
boolean found = false;
3927

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+
}
4634

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+
}
5038

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+
}
5543

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+
}
6250

63-
}
64-
return result;
65-
}
51+
}
52+
return result;
53+
}
6654

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+
}
8573
}

0 commit comments

Comments
 (0)