-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathRemoveInvalidParentheses301.java
More file actions
304 lines (250 loc) · 9.48 KB
/
RemoveInvalidParentheses301.java
File metadata and controls
304 lines (250 loc) · 9.48 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Remove the minimum number of invalid parentheses in order to make the input
* string valid. Return all possible results.
*
* Note: The input string may contain letters other than the parentheses ( and ).
*
* Examples:
* "()())()" -> ["()()()", "(())()"]
* "(a)())()" -> ["(a)()()", "(a())()"]
* ")(" -> [""]
*/
/**
* A review of all solutions (BFS and DFS)
* https://leetcode.com/problems/remove-invalid-parentheses/discuss/75038/Evolve-from-intuitive-solution-to-optimal-a-review-of-all-solutions
*
* There are three challenges:
* - Remove minimum parenthesis
* - The result is valid
* - Without duplicates.
*
*/
public class RemoveInvalidParentheses301 {
public List<String> removeInvalidParentheses(String s) {
Set<String> results = new HashSet<>();
int L = s.length();
if (L == 0) return Arrays.asList("");
Stack<Character> st = new Stack<>();
StringBuilder sb = new StringBuilder("");
int maxLength = helper(s, 0, st, sb, L, results);
return setToList(results, maxLength);
}
private int helper(String s, int level, Stack<Character> st, StringBuilder sb, int L, Set<String> results) {
int maxLength = 0;
if (level == L) {
if (st.size() == 0) {
maxLength = Math.max(maxLength, sb.length());
results.add(sb.toString());
}
return maxLength;
}
char c = s.charAt(level);
int localMax = 0;
if (c >= 'a' && c <= 'z') {
sb.append(c);
localMax = helper(s, level+1, st, sb, L, results);
maxLength = Math.max(maxLength, localMax);
sb.deleteCharAt(sb.length()-1);
} else if (c == '(') {
st.push('(');
sb.append('(');
localMax = helper(s, level+1, st, sb, L, results);
maxLength = Math.max(maxLength, localMax);
st.pop();
sb.deleteCharAt(sb.length()-1);
localMax = helper(s, level+1, st, sb, L, results);
maxLength = Math.max(maxLength, localMax);
} else if (c == ')' && !st.empty() && st.peek() == '(') {
st.pop();
sb.append(c);
localMax = helper(s, level+1, st, sb, L, results);
maxLength = Math.max(maxLength, localMax);
st.push('(');
sb.deleteCharAt(sb.length()-1);
localMax = helper(s, level+1, st, sb, L, results);
maxLength = Math.max(maxLength, localMax);
} else {
localMax = helper(s, level+1, st, sb, L, results);
maxLength = Math.max(maxLength, localMax);
}
return maxLength;
}
private List<String> setToList(Set<String> set, int maxLength) {
List<String> results = new ArrayList<>();
for (String s: set) {
if (s.length() == maxLength) {
results.add(s);
}
}
return results;
}
/**
* https://discuss.leetcode.com/topic/34875/easy-short-concise-and-fast-java-dfs-3-ms-solution
*/
public List<String> removeInvalidParentheses2(String s) {
List<String> ans = new ArrayList<>();
remove(s, ans, 0, 0, new char[]{'(', ')'});
return ans;
}
public void remove(String s, List<String> ans, int last_i, int last_j, char[] par) {
for (int stack = 0, i = last_i; i < s.length(); ++i) {
if (s.charAt(i) == par[0]) stack++;
if (s.charAt(i) == par[1]) stack--;
if (stack >= 0) continue;
for (int j = last_j; j <= i; ++j)
if (s.charAt(j) == par[1] && (j == last_j || s.charAt(j - 1) != par[1]))
remove(s.substring(0, j) + s.substring(j + 1, s.length()), ans, i, j, par);
return;
}
String reversed = new StringBuilder(s).reverse().toString();
if (par[0] == '(') // finished left to right
remove(reversed, ans, 0, 0, new char[]{')', '('});
else // finished right to left
ans.add(reversed);
}
/**
* https://discuss.leetcode.com/topic/28827/share-my-java-bfs-solution
*/
public List<String> removeInvalidParentheses3(String s) {
List<String> res = new ArrayList<>();
// sanity check
if (s == null) return res;
Set<String> visited = new HashSet<>();
Queue<String> queue = new LinkedList<>();
// initialize
queue.add(s);
visited.add(s);
boolean found = false;
while (!queue.isEmpty()) {
s = queue.poll();
if (isValid(s)) {
// found an answer, add to the result
res.add(s);
found = true;
}
if (found) continue;
// generate all possible states
for (int i = 0; i < s.length(); i++) {
// we only try to remove left or right paren
if (s.charAt(i) != '(' && s.charAt(i) != ')') continue;
String t = s.substring(0, i) + s.substring(i + 1);
if (!visited.contains(t)) {
// for each state, if it's not visited, add it to the queue
queue.add(t);
visited.add(t);
}
}
}
return res;
}
// helper function checks if string s contains valid parantheses
boolean isValid(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') count++;
if (c == ')' && count-- == 0) return false;
}
return count == 0;
}
/**
* https://discuss.leetcode.com/topic/30743/easiest-9ms-java-solution
*/
public List<String> removeInvalidParentheses4(String s) {
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++;
}
}
}
Set<String> res = new HashSet<>();
dfs(s, 0, res, new StringBuilder(), rmL, rmR, 0);
return new ArrayList<String>(res);
}
public void dfs(String s, int i, Set<String> res, StringBuilder sb, int rmL, int rmR, int open) {
if (rmL < 0 || rmR < 0 || open < 0) {
return;
}
if (i == s.length()) {
if (rmL == 0 && rmR == 0 && open == 0) {
res.add(sb.toString());
}
return;
}
char c = s.charAt(i);
int len = sb.length();
if (c == '(') {
dfs(s, i + 1, res, sb, rmL - 1, rmR, open); // not use (
dfs(s, i + 1, res, sb.append(c), rmL, rmR, open + 1); // use (
} else if (c == ')') {
dfs(s, i + 1, res, sb, rmL, rmR - 1, open); // not use )
dfs(s, i + 1, res, sb.append(c), rmL, rmR, open - 1); // use )
} else {
dfs(s, i + 1, res, sb.append(c), rmL, rmR, open);
}
sb.setLength(len);
}
/**
* https://leetcode.com/problems/remove-invalid-parentheses/discuss/75095/Java-optimized-DFS-solution-3-ms
*/
public List<String> removeInvalidParentheses5(String s) {
int count = 0, openN = 0, closeN = 0;
// calculate the total numbers of opening and closing parentheses
// that need to be removed in the final solution
for (char c : s.toCharArray()) {
if (c == '(') {
count++;
} else if (c == ')') {
if (count == 0) closeN++;
else count--;
}
}
openN = count;
count = 0;
if (openN == 0 && closeN == 0) return Arrays.asList(s);
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
dfs(s.toCharArray(), 0, count, openN, closeN, result, sb);
return result;
}
private void dfs(char[] s, int p, int count, int openN, int closeN, List<String> result, StringBuilder sb) {
if (count < 0) return; // the parentheses is invalid
if (p == s.length) {
if (openN == 0 && closeN == 0) { // the minimum number of invalid parentheses have been removed
result.add(sb.toString());
}
return;
}
if (s[p] != '(' && s[p] != ')') {
sb.append(s[p]);
dfs(s, p + 1, count, openN, closeN, result, sb);
sb.deleteCharAt(sb.length() - 1);
} else if (s[p] == '(') {
int i = 1;
while (p + i < s.length && s[p + i] == '(') i++; // use while loop to avoid duplicate result in DFS, instead of using HashSet
sb.append(s, p, i);
dfs(s, p + i, count + i, openN, closeN, result, sb);
sb.delete(sb.length() - i, sb.length());
if (openN > 0) {
// remove the current opening parenthesis
dfs(s, p + 1, count, openN - 1, closeN, result, sb);
}
} else {
int i = 1;
while (p + i < s.length && s[p + i] == ')') i++; // use while loop to avoid duplicate result in DFS, instead of using HashSet
sb.append(s, p, i);
dfs(s, p + i, count - i, openN, closeN, result, sb);
sb.delete(sb.length() - i, sb.length());
if (closeN > 0) {
// remove the current closing parenthesis
dfs(s, p + 1, count, openN, closeN - 1, result, sb);
}
}
}
}