Repository navigation
Expand file tree
/
Copy path1096.cpp
More file actions
74 lines (73 loc) · 2.18 KB
/
Copy path1096.cpp
File metadata and controls
74 lines (73 loc) · 2.18 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution {
public:
unordered_set<string> cross(unordered_set<string>& a, unordered_set<string>& b) {
if (a.size() == 0) a.insert("");
if (b.size() == 0) b.insert("");
unordered_set<string> res;
for (auto i : a) {
for (auto j : b) {
res.insert(i + j);
}
}
return res;
}
unordered_set<string> merge(unordered_set<string>& a, unordered_set<string>& b) {
unordered_set<string> res;
for (auto i : a) {
res.insert(i);
}
for (auto i : b) {
res.insert(i);
}
return res;
}
vector<string> braceExpansionII(string expression) {
string s = "";
for (auto& c : expression) {
if (isalpha(c)) {
s.push_back('{');
s.push_back(c);
s.push_back('}');
}
else s.push_back(c);
}
unordered_set<string> current;
stack<unordered_set<string>> stStr;
stack<int> stOp;
int n = s.size();
for (int i = 0; i < n; ++i) {
if (s[i] == '{') {
stStr.push(current);
stOp.push(0);
current.clear();
}
else if (s[i] == ',') {
stStr.push(current);
stOp.push(1);
current.clear();
}
else if (s[i] == '}') {
while (stOp.top() == 1) {
current = merge(stStr.top(), current);
stStr.pop();
stOp.pop();
}
if (stOp.top() == 0) {
current = cross(stStr.top(), current);
stStr.pop();
stOp.pop();
}
}
else {
int start = i;
while (i + 1 < n && isalpha(s[i + 1])) i++;
// [start, i] is valid
string sub = s.substr(start, i + 1 - start);
current.insert(sub);
}
}
vector<string> res(current.begin(), current.end());
sort(res.begin(), res.end());
return res;
}
};