-
Notifications
You must be signed in to change notification settings - Fork 481
/
1096.cpp
50 lines (48 loc) · 1.12 KB
/
1096.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
class Solution
{
public:
vector<string> braceExpansionII(string expression)
{
this->s = expression + "}";
int i = 0;
auto res = solve(i);
return res;
}
private:
string s;
vector<string> _merge(vector<string>& a, vector<string>& b)
{
vector<string> res;
for (auto& l : a)
{
for (auto & r : b)
{
res.push_back(l + r);
}
}
sort(res.begin(), res.end());
return res;
}
vector<string> solve(int& i)
{
if (s[i] != '{') {
vector<string> L = {string(1, s[i])};
i++;
if (s[i] == '}' || s[i] == ',') return L;
auto R = solve(i);
return _merge(L, R);
}
set<string> all;
while (s[i] != '}')
{
++i;
auto R = solve(i);
for (auto& it : R) all.insert(it);
}
vector<string> L(all.begin(), all.end());
++i;
if (s[i] == '}' || s[i] == ',') return L;
auto R = solve(i);
return _merge(L, R);
}
};