-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
42 lines (41 loc) · 1.21 KB
/
Solution.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
class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
unordered_set<string> ans;
int l = 0, r = 0, n = s.size();
for (char& c : s) {
if (c == '(') {
++l;
} else if (c == ')') {
if (l) {
--l;
} else {
++r;
}
}
}
function<void(int, int, int, int, int, string)> dfs;
dfs = [&](int i, int l, int r, int lcnt, int rcnt, string t) {
if (i == n) {
if (l == 0 && r == 0) {
ans.insert(t);
}
return;
}
if (n - i < l + r || lcnt < rcnt) {
return;
}
if (s[i] == '(' && l) {
dfs(i + 1, l - 1, r, lcnt, rcnt, t);
}
if (s[i] == ')' && r) {
dfs(i + 1, l, r - 1, lcnt, rcnt, t);
}
int x = s[i] == '(' ? 1 : 0;
int y = s[i] == ')' ? 1 : 0;
dfs(i + 1, l, r, lcnt + x, rcnt + y, t + s[i]);
};
dfs(0, l, r, 0, 0, "");
return vector<string>(ans.begin(), ans.end());
}
};