Skip to content

Commit 6a19059

Browse files
refactor 301
1 parent ccdc604 commit 6a19059

File tree

1 file changed

+47
-42
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+47
-42
lines changed

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

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.Set;
99

1010
/**
11+
* 301. Remove Invalid Parentheses
12+
*
1113
* Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
1214
1315
Note: The input string may contain letters other than the parentheses ( and ).
@@ -19,62 +21,65 @@
1921
*/
2022
public class _301 {
2123

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 {
2725

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

31-
q.offer(s);
32-
visited.add(s);
32+
Set<String> visited = new HashSet();
33+
Queue<String> q = new LinkedList();
3334

34-
boolean found = false;
35+
q.offer(s);
36+
visited.add(s);
3537

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;
4239

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

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
5049
}
5150

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+
}
5661
}
57-
}
5862

63+
}
64+
return result;
5965
}
60-
return result;
61-
}
6266

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+
}
7580
}
7681
}
82+
return count == 0;
7783
}
78-
return count == 0;
7984
}
8085
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy