-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path301.cpp
More file actions
executable file
·69 lines (69 loc) · 2.76 KB
/
301.cpp
File metadata and controls
executable file
·69 lines (69 loc) · 2.76 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution
{
string s;
set<pair<string, int>> vis;
vector<string> res;
void dfs(string t, int left, int right, size_t pos)
{
auto state = make_pair(t, pos);
if (vis.count(state)) {
return;
}
vis.insert(state);
bool valid = true;
int cnt = 0;
for (auto c : t) {
if (c == '(') {
++cnt;
} else if (c == ')') {
--cnt;
if (cnt < 0) {
valid = false;
break;
}
}
}
if (!valid) {
return;
} else if (pos == s.size()) {
res.push_back(t);
return;
}
t += s[pos];
if (s[pos] == '(') {
++left;
if (left <= right) {
dfs(t, left, right, pos + 1);
return;
}
int n = (int)t.size();
for (int i = 0; i < n; ++i) {
if (t[i] == '(') {
dfs(t.substr(0, i) + t.substr(i + 1), left - 1, right, pos + 1);
}
}
} else if (s[pos] == ')') {
if (left > 0 && left <= right) {
dfs(t, left - 1, right - 1, pos + 1);
return;
}
--right;
int n = (int)t.size();
for (int i = 0; i < n; ++i) {
if (t[i] == ')') {
dfs(t.substr(0, i) + t.substr(i + 1), left, right, pos + 1);
}
}
} else {
dfs(t, left, right, pos + 1);
}
}
public:
vector<string> removeInvalidParentheses(string s)
{
this->s = s;
int cnt = count_if(s.begin(), s.end(), [](char c) {return c == ')'; });
dfs("", 0, cnt, 0);
return res;
}
};