-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathRemove Invalid Parentheses.cpp
131 lines (116 loc) · 3.87 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Solution 1: BFS
class Solution {
int GetErrors(const string &s) {
stack<char> mystack;
int valid = 0;
for (char ch : s) {
if (ch == '(') {
mystack.push(ch);
} else if (ch == ')') {
if (!mystack.empty()) {
valid += 2;
mystack.pop();
}
} else {
valid++;
}
}
return s.length() - valid;
}
public:
vector<string> removeInvalidParentheses(string s) {
queue<string> myqueue;
unordered_set<string> visited;
myqueue.push(s);
visited.insert(s);
int errors = GetErrors(s);
while (!myqueue.empty() && errors > 0) {
int queue_size = myqueue.size();
for (int k = 0; k < queue_size; k++) {
string str = myqueue.front();
myqueue.pop();
int i = 0;
while (i < str.length()) {
if (str[i] != '(' && str[i] != ')') {
i++;
continue;
}
string tmp = str.substr(0,i) + str.substr(i+1);
if (visited.count(tmp) == 0) {
int current_errors = GetErrors(tmp);
if (current_errors < errors) {
myqueue.push(tmp);
visited.insert(tmp);
}
}
while (i+1 < str.length() && str[i] == str[i+1]) i++;
i++;
}
}
errors--;
}
vector<string> result;
while (!myqueue.empty()) {
string tmp = myqueue.front();
myqueue.pop();
result.push_back(tmp);
}
return result;
}
};
// Solution 2: DFS
class Solution {
int GetErrors(const string &s) {
stack<char> mystack;
int valid = 0;
for (char ch : s) {
if (ch == '(') {
mystack.push(ch);
} else if (ch == ')') {
if (!mystack.empty()) {
valid += 2;
mystack.pop();
}
} else {
valid++;
}
}
return s.length() - valid;
}
public:
void dfs(const string& s, int ind, int validLen, int leftCnt, int rightCnt, string cur, unordered_set<string>& visited, unordered_set<string>& res_set) {
if (cur.size() == validLen) {
if (leftCnt == rightCnt) {
res_set.insert(cur);
}
return;
}
if (ind >= s.size()) {
return;
}
string key = to_string(ind) + cur;
if (visited.count(key) > 0) return;
visited.insert(key);
if (s[ind] == '(' || s[ind] == ')') {
if (cur.length() + (s.size() - ind) > validLen) {
dfs(s, ind+1, validLen, leftCnt, rightCnt, cur, visited, res_set);
}
if (s[ind] == '(') {
if (leftCnt < validLen / 2) {
dfs(s, ind+1, validLen, leftCnt+1, rightCnt, cur + '(', visited, res_set);
}
} else if (leftCnt > rightCnt) {
dfs(s, ind+1, validLen, leftCnt, rightCnt+1, cur + ')', visited, res_set);
}
} else {
dfs(s, ind+1, validLen, leftCnt, rightCnt, cur + s[ind], visited, res_set);
}
}
vector<string> removeInvalidParentheses(string s) {
int validLen = s.size() - GetErrors(s);
unordered_set<string> visited;
unordered_set<string> res_set;
dfs(s, 0, validLen, 0, 0, "", visited, res_set);
return vector<string>(res_set.begin(), res_set.end());
}
};