forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
119 lines (92 loc) · 2.87 KB
/
main2.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
/// Source : https://leetcode.com/problems/remove-invalid-parentheses/description/
/// Author : liuyubobobo
/// Time : 2018-11-02
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
/// Calculate how many '(' and ')' are there
/// int the result maximum len expression first
/// Time Complexity: O(2^n)
/// Space Complexity: O(n)
class Solution {
private:
int n, maxlen;
public:
vector<string> removeInvalidParentheses(string s) {
n = s.size();
int total_left = 0, total_right = 0, l = 0, r = 0;
for(char c: s){
if(c == '(') total_left ++;
else if(c == ')') total_right ++;
if(c == '(') l ++;
else if(c == ')'){
if(l > 0) l --;
else r ++;
}
}
int rem_left = total_left - l, rem_right = total_right - r;
// cout << "rem_left : " << rem_left << " ; rem_right : " << rem_right << endl;
maxlen = s.size() - l - r;
unordered_set<string> res;
dfs(s, 0, rem_left, rem_right, "", res);
return vector<string>(res.begin(), res.end());
}
private:
void dfs(const string& s, int index, int rem_left, int rem_right,
const string& cur, unordered_set<string>& res){
if(cur.size() == maxlen){
if(!rem_left && !rem_right)
res.insert(cur);
return;
}
if(index == n || cur.size() > maxlen)
return;
if(s[index] != '(' && s[index] != ')')
dfs(s, index + 1, rem_left, rem_right, cur + s[index], res);
else if(s[index] == '('){
if(rem_left)
dfs(s, index + 1, rem_left - 1, rem_right, cur + s[index], res);
dfs(s, index + 1, rem_left, rem_right, cur, res);
}
else{
if(rem_right && rem_right > rem_left)
dfs(s, index + 1, rem_left, rem_right - 1, cur + s[index], res);
dfs(s, index + 1, rem_left, rem_right, cur, res);
}
}
};
void print_vec(const vector<string>& vec){
cout << vec.size() << " : ";
for(const string& e: vec)
cout << e << " ";
cout << endl;
}
int main() {
string s1 = "()())()";
print_vec(Solution().removeInvalidParentheses(s1));
// 6
// (())() ()()()
string s2 = "(a)())()";
print_vec(Solution().removeInvalidParentheses(s2));
// 7
// (a())() (a)()()
string s3 = ")(";
print_vec(Solution().removeInvalidParentheses(s3));
// 0
string s4 = "n";
print_vec(Solution().removeInvalidParentheses(s4));
// 1
// n
string s5 = "x(";
print_vec(Solution().removeInvalidParentheses(s5));
// 1
// x
string s6 = "((";
print_vec(Solution().removeInvalidParentheses(s6));
// 0
string s7 = "(()";
print_vec(Solution().removeInvalidParentheses(s7));
// 2
return 0;
}