Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3982b6d

Browse files
solves minimum removals to make valid parentheses inj java
1 parent e1dd58f commit 3982b6d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@
504504
| 1232 | [Check If It Is A Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line) | [![Java](assets/java.png)](src/CheckIfItIsASStraightLine.java) | |
505505
| 1237 | [Find Positive Integer Solutions for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation) | | |
506506
| 1243 | 🔒 [Array Transformation](https://leetcode.com/problems/array-transformation) | | |
507+
| 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses) | [![Java](assets/java.png)](src/MinimumRemoveToMakeValidParentheses.java) | |
507508
| 1252 | [Cells With Odd Values In Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix) | [![Java](assets/java.png)](src/CellsWithOddValuesInMatrix.java) | |
508509
| 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid) | [![Java](assets/java.png)](src/Shift2DGrid.java) | |
509510
| 1266 | [Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points) | [![Java](assets/java.png)](src/MinimumTimeVisitingAllPoints.java) | |
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses
2+
// T: O(n)
3+
// S: O(n)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.Stack;
10+
11+
public class MinimumRemoveToMakeValidParentheses {
12+
private static final record OpeningBrace(int index) { }
13+
14+
public String minRemoveToMakeValid(String s) {
15+
final Stack<OpeningBrace> stack = new Stack<>();
16+
final List<Integer> extraClosingBraceIndices = new ArrayList<>();
17+
char c;
18+
for (int i = 0 ; i < s.length() ; i++) {
19+
c = s.charAt(i);
20+
if (c == ')') {
21+
if (!stack.isEmpty()) {
22+
stack.pop();
23+
} else extraClosingBraceIndices.add(i);
24+
} else if (c == '(') stack.push(new OpeningBrace(i));
25+
}
26+
final Set<Integer> errorIndices = compileErrorIndicesFrom(extraClosingBraceIndices, stack);
27+
StringBuilder result = new StringBuilder();
28+
for (int i = 0; i < s.length() ; i++) {
29+
if (!errorIndices.contains(i)) {
30+
result.append(s.charAt(i));
31+
}
32+
}
33+
return result.toString();
34+
}
35+
36+
private Set<Integer> compileErrorIndicesFrom(List<Integer> wrongClosingBraces, Stack<OpeningBrace> openingBraces) {
37+
Set<Integer> result = new HashSet<>();
38+
while (!openingBraces.isEmpty()) {
39+
result.add(openingBraces.pop().index);
40+
}
41+
result.addAll(wrongClosingBraces);
42+
return result;
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /