-
Notifications
You must be signed in to change notification settings - Fork 0
/
braceexpansion2.cpp
61 lines (60 loc) · 1.8 KB
/
braceexpansion2.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
51
52
53
54
55
56
57
58
59
60
61
class Solution {
public:
unordered_set<string> helper(string &exp, int &ind) {
int n = exp.length();
string word;
unordered_set<string> ans, smaller, temp;
while (ind < n) {
if (exp[ind] == '{') {
ind++;
smaller = helper(exp,ind);
ind++;
if (smaller.size()) {
if (!ans.size())
ans = smaller;
else {
temp = ans;
ans.clear();
for (auto &x : temp) {
for (auto &y : smaller)
ans.insert(x+y);
}
}
}
}
else if (exp[ind] == ',') {
ind++;
smaller = helper(exp,ind);
for (auto &x : smaller)
ans.insert(x);
}
else if (exp[ind] == '}')
return ans;
else {
word = "";
while (ind < n && exp[ind] >= 'a' && exp[ind] <= 'z') {
word += exp[ind];
ind++;
}
if (ans.size()) {
temp = ans;
ans.clear();
for (auto &x : temp)
ans.insert(x+word);
}
else
ans.insert(word);
}
}
return ans;
}
vector<string> braceExpansionII(string expression) {
int id = 0;
vector<string> ans;
unordered_set<string> all = helper(expression,id);
for (auto &x : all)
ans.push_back(x);
sort(ans.begin(),ans.end());
return ans;
}
};