Minimum Remove to Make Valid Parentheses LeetCode solution in C++

1
class Solution {
2
public:
3
string minRemoveToMakeValid(string s) {
4
int count = 0;
5
for (int i = 0; i < s.size(); i++) {
6
if (s[i] == '(') {
7
count++;
8
}
9
if (s[i] == ')') {
10
if (count == 0) {
11
s[i] = '*';
12
} else {
13
count--;
14
}
15
}
16
}
17
if (count == 0) {
18
s.erase(remove(s.begin(), s.end(), '*'), s.end());
19
return s;
20
}
21
for (int i = s.size() - 1; i >= 0; i--) {
22
if (s[i] == '(') {
23
s[i] = '*';
24
count--;
25
}
26
if (count == 0) {
27
s.erase(remove(s.begin(), s.end(), '*'), s.end());
28
return s;
29
}
30
}
31
return s;
32
}
33
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0