-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1096.cpp
More file actions
58 lines (58 loc) · 1.71 KB
/
1096.cpp
File metadata and controls
58 lines (58 loc) · 1.71 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
class Solution
{
int len;
public:
vector<string> braceExpansionII(string expression)
{
int start = 0;
len = expression.size();
auto k = dfs(expression,start);
vector<string> res;
for(auto& x:k) res.push_back(x);
sort(res.begin(),res.end());
return res;
}
unordered_set<string> dfs(string& expression,int& start)
{
unordered_set<string> res,t;
while(start < len && expression[start] != '}'){
if(expression[start] == '{'){
auto p = dfs(expression,++start);
if(t.empty()) t = p;
else{
unordered_set<string> temp;
for(auto& x:t) {
for(auto& y:p) {
temp.insert((string)x+y);
}
}
t = temp;
}
}
else if(expression[start] == ',') {
for(auto& x:t) {
res.insert(x);
}
t.clear();
++start;
}
else{
string s;
while(start < len && expression[start] >= 'a' && expression[start] <= 'z') s.push_back(expression[start++]);
if(t.empty()) t.insert(s);
else{
unordered_set<string> temp;
for(auto& x:t){
temp.insert(x+s);
}
t = temp;
}
}
if(start >= len || expression[start] == '}'){
for(auto& x:t) res.insert(x);
}
}
++start;
return res;
}
};