Skip to content

Commit 38291e5

Browse files
add 1249
1 parent c3de63f commit 38291e5

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ _If you like this project, please leave me a star._ ★
244244
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
245245
|1258|[Synonymous Sentences](https://leetcode.com/problems/synonymous-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1258.java) ||Medium|Backtracking|
246246
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | |Easy||
247-
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | |Easy||
247+
|1249|[Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1249.java) | |Medium|String, Stack|
248248
|1243|[Array Transformation](https://leetcode.com/problems/array-transformation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1243.java) | [:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs)|Easy||
249+
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | |Easy||
249250
|1232|[Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1232.java) | [:tv:](https://www.youtube.com/watch?v=_tfiTQNZCbs) |Easy||
250251
|1228|[Missing Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1228.java) | |Easy||
251252
|1221|[Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1221.java) | |Easy|Greedy|
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Stack;
4+
35
public class _1249 {
46
public static class Solution1 {
57
public String minRemoveToMakeValid(String s) {
6-
//TODO: implement it
7-
return "";
8+
Stack<Character> stack = new Stack<>();
9+
int leftParen = 0;
10+
int rightParen = 0;
11+
for (char c : s.toCharArray()) {
12+
if (c == '(') {
13+
stack.push(c);
14+
leftParen++;
15+
} else if (c == ')') {
16+
if (leftParen > rightParen) {
17+
stack.push(c);
18+
rightParen++;
19+
}
20+
} else {
21+
stack.push(c);
22+
}
23+
}
24+
StringBuilder sb = new StringBuilder();
25+
int diff = leftParen - rightParen;
26+
while (!stack.isEmpty()) {
27+
if (stack.peek() == '(') {
28+
if (diff-- > 0) {
29+
stack.pop();
30+
} else {
31+
sb.append(stack.pop());
32+
}
33+
} else {
34+
sb.append(stack.pop());
35+
}
36+
}
37+
return sb.reverse().toString();
838
}
939
}
1040
}

0 commit comments

Comments
 (0)