-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveInvalidParentheses.java
More file actions
145 lines (136 loc) · 5.4 KB
/
Copy pathRemoveInvalidParentheses.java
File metadata and controls
145 lines (136 loc) · 5.4 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
public class RemoveInvalidParentheses{
// BFS: avoid duplicates rather than using set
// possible duplicates:
// 1. consequtive '(' and ')', remove any of than is the same
// 2. remove two positions but in different order is the same
// 3. remove '(' than ')' is not necessary
// problems:
// 1. repetitive valid checking
// 2. do not have a remove strategy
// 15 ms
public List<String> removeInvalidParentheses(String s) {
ArrayList<String> result = new ArrayList<String>();
if(isValid(s))
result.add(s);
else{
ArrayList<Tuple> cur = new ArrayList<Tuple>();
cur.add(new Tuple(new StringBuilder(s), 0, ')'));
while(!cur.isEmpty()){
ArrayList<Tuple> next = new ArrayList<Tuple>();
for(Tuple t : cur){
String temp = t.ss;
for(int i = t.nextRemoveIndex; i < temp.length(); i++){
char c = temp.charAt(i);
if(c != ')' && c != '(') continue;
if(i != t.nextRemoveIndex && c == temp.charAt(i-1)) continue;
if(c == ')' && t.lastRemove == '(') continue;
String st = temp.substring(0, i) + temp.substring(i + 1);
if(isValid(st))
result.add(st);
else if(result.isEmpty())
next.add(new Tuple(st, i, c));
}
}
if(!result.isEmpty()) break;
cur = next;
}
}
return result;
}
public boolean isValid(String s){
int net = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '(')
net ++;
else if(s.charAt(i) == ')'){
if(net -- == 0)
return false;
}
}
return net == 0;
}
// DFS: always do the right step, always make sure the prefix is valid
// do from left to right, than reverse (so that dp can be applied)
// duplicates: consequtive '(' and ')', remove in different order
// similar to the BFS, but can avoid repetitive valid checking
// 3 ms
public List<String> removeInvalidParentheses(String s) {
ArrayList<String> result = new ArrayList<String>();
char[] chs = {'(', ')'};
removeInvalidParentheses(s, 0, 0, result, chs);
return result;
}
public void removeInvalidParentheses(String s, int pos, int remove, ArrayList<String> result, char[] chs){
int count = 0;
for(int i = pos; i < s.length(); i++){
if(s.charAt(i) == chs[0]) count ++;
else if(s.charAt(i) == chs[1]) count --;
if(count < 0){
for(int j = remove; j <= i; j++){
if(s.charAt(j) == chs[1] && (j == remove || s.charAt(j) != s.charAt(j - 1)))
removeInvalidParentheses(s.substring(0, j) + s.substring(j + 1), i, j, result, chs); }
return;
}
}
s = new StringBuilder(s).reverse().toString();
if(chs[0] == ')')
result.add(s);
else{
char[] chsx = {')', '('};
removeInvalidParentheses(s, 0, 0, result, chsx);
}
}
// DFS: preprocess to know the number of '(' and ')' to remove
// dynamic valid checking
// like bit operation: remove from smaller index
// duplicates: consequtive '(' and ')'
// 3 ms
public List<String> removeInvalidParentheses(String s) {
ArrayList<String> result = new ArrayList<String>();
int rmL = 0, rmR = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '(')
rmL ++;
else if(s.charAt(i) == ')'){
if(rmL > 0)
rmL --;
else
rmR ++;
}
}
remove(s, 0, rmL, rmR, 0, result, new StringBuilder(), -1);
return result;
}
public void remove(String s, int pos, int rmL, int rmR, int open, ArrayList<String> result, StringBuilder sb, int lastRemove){
if(pos == s.length() && rmL == 0 && rmR == 0 && open == 0){
result.add(sb.toString());
return;
}
else if(pos == s.length() || rmL < 0 || rmR < 0 || open < 0)
return;
char c = s.charAt(pos);
int len = sb.length();
if(c == '('){
if(pos == 0 || s.charAt(pos - 1) != c || lastRemove == pos - 1)
remove(s, pos + 1, rmL - 1, rmR, open, result, sb, pos);
remove(s, pos + 1, rmL, rmR, open + 1, result, sb.append(c), lastRemove);
} else if(c == ')'){
if((pos == 0 || s.charAt(pos - 1) != c || lastRemove == pos - 1) && (lastRemove < 0 || s.charAt(lastRemove) != '('))
remove(s, pos + 1, rmL, rmR - 1, open, result, sb, pos);
remove(s, pos + 1, rmL, rmR, open - 1, result, sb.append(c), lastRemove);
} else {
remove(s, pos + 1, rmL, rmR, open, result, sb.append(c), lastRemove);
}
sb.setLength(len);
}
}
class Tuple{
StringBuilder ss;
int nextRemoveIndex;
char lastRemove;
public Tuple(String ss, int nextRemoveIndex, char lastRemove){
this.ss = ss;
this.nextRemoveIndex = nextRemoveIndex;
this.lastRemove = lastRemove;
}
}