-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1249.cpp
More file actions
47 lines (47 loc) · 1.25 KB
/
1249.cpp
File metadata and controls
47 lines (47 loc) · 1.25 KB
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
45
46
47
class Solution {
public:
string minRemoveToMakeValid(string s) {
string tempLeft = "";
int cnt = 0;
int n = s.size();
for (int i = 0; i < n; ++i) {
if (s[i] == '(') {
if (cnt >= 0) {
tempLeft.push_back(s[i]);
cnt++;
}
}
else if (s[i] == ')') {
if (cnt > 0) {
tempLeft.push_back(s[i]);
cnt--;
}
}
else {
tempLeft.push_back(s[i]);
}
}
string tempRight = "";
cnt = 0;
n = tempLeft.size();
for (int i = n - 1; i >= 0; --i) {
if (tempLeft[i] == ')') {
if (cnt >= 0) {
tempRight.push_back(tempLeft[i]);
cnt++;
}
}
else if (tempLeft[i] == '(') {
if (cnt > 0) {
tempRight.push_back(tempLeft[i]);
cnt--;
}
}
else {
tempRight.push_back(tempLeft[i]);
}
}
reverse(tempRight.begin(), tempRight.end());
return tempRight;
}
};