-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMinimumRemoveToMakeValidParentheses.java
44 lines (40 loc) · 1.5 KB
/
MinimumRemoveToMakeValidParentheses.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
// https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses
// T: O(n)
// S: O(n)
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
public class MinimumRemoveToMakeValidParentheses {
private static final record OpeningBrace(int index) { }
public String minRemoveToMakeValid(String s) {
final Stack<OpeningBrace> stack = new Stack<>();
final List<Integer> extraClosingBraceIndices = new ArrayList<>();
char c;
for (int i = 0 ; i < s.length() ; i++) {
c = s.charAt(i);
if (c == ')') {
if (!stack.isEmpty()) {
stack.pop();
} else extraClosingBraceIndices.add(i);
} else if (c == '(') stack.push(new OpeningBrace(i));
}
final Set<Integer> errorIndices = compileErrorIndicesFrom(extraClosingBraceIndices, stack);
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length() ; i++) {
if (!errorIndices.contains(i)) {
result.append(s.charAt(i));
}
}
return result.toString();
}
private Set<Integer> compileErrorIndicesFrom(List<Integer> wrongClosingBraces, Stack<OpeningBrace> openingBraces) {
Set<Integer> result = new HashSet<>();
while (!openingBraces.isEmpty()) {
result.add(openingBraces.pop().index);
}
result.addAll(wrongClosingBraces);
return result;
}
}