-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1096.brace-expansion-ii.cpp
More file actions
61 lines (58 loc) · 1.28 KB
/
1096.brace-expansion-ii.cpp
File metadata and controls
61 lines (58 loc) · 1.28 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
59
60
/*
* @lc app=leetcode id=1096 lang=cpp
*
* [1096] Brace Expansion II
*/
// @lc code=start
int len;
vector<string> solve(string &expression, int &index) {
vector<string> result;
vector<string> cur = {""};
while(index < len) {
char c = expression[index];
index += 1;
if(c == '}') {
break;
}
if(c == ',') {
result.insert(
result.end(),
make_move_iterator(cur.begin()),
make_move_iterator(cur.end())
);
cur = {""};
} else if(isalpha(c)) {
for(auto &s : cur) {
s.push_back(c);
}
} else if(c == '{') {
vector<string> next;
for(auto &res : solve(expression, index)) {
for(auto &s : cur) {
next.push_back(s + res);
}
}
swap(next, cur);
}
}
if(cur.front().length()) {
result.insert(
result.end(),
make_move_iterator(cur.begin()),
make_move_iterator(cur.end())
);
}
return result;
}
class Solution {
public:
vector<string> braceExpansionII(string expression) {
len = expression.length();
int index = 0;
auto answer = solve(expression, index);
sort(answer.begin(), answer.end());
answer.resize(unique(answer.begin(), answer.end()) - answer.begin());
return answer;
}
};
// @lc code=end