-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_1249.cpp
49 lines (48 loc) · 1.07 KB
/
data_1249.cpp
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
48
49
#include "headl.h"
class Solution {
public:
string minRemoveToMakeValid(string s) {
vector<int> a = { 0,0 };
vector<int> b = { 0,0 };
int n = s.size();
for (int i = 0; i < n; i++)
{
if (s[i] == ')')
{
if (a[0] > a[1]) a[1]++;
}
else if (s[i] == '(')
{
a[0] ++;
}
}
string ans = "";
for (int i = 0; i < n; i++)
{
if (s[i] == ')')
{
if (b[0] > b[1])
{
b[1] ++;
ans.push_back(s[i]);
}
}
else if (s[i] == '(')
{
if (b[0] < a[1])
{
ans.push_back(s[i]);
b[0] ++;
}
}
else ans.push_back(s[i]);
}
return ans;
}
};
int main()
{
freopen("test_in.txt", "r", stdin);
freopen("test_out.txt", "w", stdout);
Solution* a = new Solution();
}