-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy paths2.cpp
More file actions
44 lines (44 loc) · 1.23 KB
/
s2.cpp
File metadata and controls
44 lines (44 loc) · 1.23 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
// OJ: https://leetcode.com/problems/brace-expansion-ii/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N^2)
class Solution {
int i = 0;
vector<string> handleList(string &s) {
++i; // {
vector<string> ans;
while (s[i] != '}') {
auto st = dfs(s);
for (auto &a : st)
ans.push_back(a);
if (s[i] == ',') ++i;
}
++i; // }
return ans;
}
vector<string> dfs(string &s) {
vector<string> ans{ "" };
while (i < s.size() && s[i] != ',' && s[i] != '}') {
if (s[i] != '{') {
for (auto &a : ans) a += s[i];
++i;
} else {
auto list = handleList(s);
vector<string> tmp;
for (auto &a : ans)
for (auto &b : list)
tmp.push_back(a + b);
swap(tmp, ans);
}
}
return ans;
}
public:
vector<string> braceExpansionII(string s) {
auto st = dfs(s);
vector<string> ans(st.begin(), st.end());
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
return ans;
}
};