-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
63 lines (60 loc) · 1.66 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
string minRemoveToMakeValid(string s) {
int size = s.length();
string outStr;
int count = 0;
vector<int> rightIdx;
vector<int> leftIdx;
for (int i = 0; i < size; i++) {
switch (s[i]) {
case '(':
leftIdx.push_back(i);
count++;
break;
case ')':
if (count != 0) {
rightIdx.push_back(i);
count--;
}
break;
default:
break;
}
}
for (int i = 0; i < count; i++) leftIdx.pop_back();
int left = 0, right = 0;
int leftSize = leftIdx.size(), rightSize = rightIdx.size();
for (int i = 0; i < size; i++) {
switch (s[i]) {
case '(':
if (left < leftSize && i == leftIdx[left]) {
outStr.push_back(s[i]);
left++;
}
break;
case ')':
if (right < rightSize && i == rightIdx[right]) {
outStr.push_back(s[i]);
right++;
}
break;
default:
outStr.push_back(s[i]);
break;
}
}
return outStr;
}
};
int main(void) {
string s;
cin >> s;
Solution sol;
cout << sol.minRemoveToMakeValid(s) << endl;
}