-
Notifications
You must be signed in to change notification settings - Fork 1
/
301 Remove Invalid Parentheses .cpp
65 lines (65 loc) · 2 KB
/
301 Remove Invalid Parentheses .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
64
65
class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
vector<string> ans;
int cL = 0, cR = 0;
for (auto ch : s) {
if (ch == '(') {
cL++;
} else if (ch == ')') {
if (cL) {
cL--;
} else {
cR++;
}
}
}
function<void(int, int, int, vector<int> &)> dfs = [&] (int dep, int cL, int cR, vector<int> &del) {
if (!cL && !cR) {
string tmp;
int j = 0;
for (int i = 0; i < s.size(); i++) {
while (j < del.size() && del[j] < i) {
j++;
}
if (j == del.size() || i != del[j]) {
tmp.push_back(s[i]);
}
}
auto sum = 0;
for (auto ch : tmp) {
if (ch == '(') {
sum++;
} else if (ch == ')') {
sum--;
}
if (sum < 0) {
return ;
}
}
ans.push_back(tmp);
return ;
}
for (int i = dep; i < s.size(); i++) {
if (s[i] == '(') {
if (cL && !cR) {
del.push_back(i);
dfs(i + 1, cL - 1, cR, del);
del.pop_back();
}
} else if (s[i] == ')') {
if (cR) {
del.push_back(i);
dfs(i + 1, cL, cR - 1, del);
del.pop_back();
}
}
}
};
vector<int> delTmp;
dfs(0, cL, cR, delTmp);
sort(begin(ans), end(ans));
ans.erase(unique(begin(ans), end(ans)), end(ans));
return ans;
}
};