-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRemoveInvalidParentheses.java
52 lines (43 loc) · 1.35 KB
/
RemoveInvalidParentheses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public List<String> removeInvalidParentheses(String s) {
List<String> list = new ArrayList<>();
int minr = getMin(s);
HashSet<String> set = new HashSet<>();
remove(s, minr, set, list);
return list;
}
public void remove(String s, int mra, HashSet<String> set, List<String> ans) {
if(mra == 0) {
if(getMin(s) == 0) {
if(!set.contains(s)) {
ans.add(s);
set.add(s);
}
}
return;
}
for(int i=0; i<s.length(); i++) {
String left = s.substring(0,i);
String right = s.substring(i+1);
remove(left+right, mra-1, set, ans);
}
}
public int getMin(String s) {
Stack<Character> st = new Stack<>();
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if(c == '(') {
st.push(c);
} else if(c == ')') {
if(st.size() == 0) {
st.push(c);
} else if(st.peek() == ')') {
st.push(c);
} else if(st.peek() == '(') {
st.pop();
}
}
}
return st.size();
}
}