/
Solution.java
35 lines (33 loc) · 1.09 KB
/
Solution.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
package g1201_1300.s1249_minimum_remove_to_make_valid_parentheses;
// #Medium #String #Stack #Data_Structure_II_Day_14_Stack_Queue
// #2022_03_12_Time_13_ms_(94.62%)_Space_51.3_MB_(53.95%)
public class Solution {
public String minRemoveToMakeValid(String s) {
int closingParantheis = 0;
for (char ch : s.toCharArray()) {
if (ch == ')') {
closingParantheis++;
}
}
StringBuilder result = new StringBuilder();
int openingParanthesis = 0;
for (char ch : s.toCharArray()) {
if (ch == ')' && openingParanthesis == 0) {
closingParantheis--;
} else {
if (ch == ')') {
openingParanthesis--;
}
if (ch == '(' && closingParantheis == 0) {
continue;
}
if (ch == '(') {
openingParanthesis++;
closingParantheis--;
}
result.append(ch);
}
}
return result.toString();
}
}