-
Notifications
You must be signed in to change notification settings - Fork 0
/
braceexpansion.cpp
43 lines (42 loc) · 1.16 KB
/
braceexpansion.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
class Solution {
public:
vector<string> expand(string S) {
int i, j, k, m, n = S.length();
int num_words = 1, num_type;
vector<vector<char> > dict;
vector<char> uno;
vector<string> ans;
for (i = 0; i < n; i++) {
if (S[i] > 96 && S[i] < 123) {
uno.push_back(S[i]);
if (i+1 >= n || S[i+1] != ',') {
dict.push_back(uno);
uno.clear();
}
}
}
for (i = 0; i < dict.size(); i++) {
num_words *= dict[i].size();
sort(dict[i].begin(),dict[i].end());
}
for (i = 0; i < num_words; i++)
ans.push_back("");
k = 0;
num_type = num_words;
while (k < dict.size()) {
i = 0;
m = dict[k].size();
while (i < num_words) {
j = 0;
while (j < num_type) {
ans[i+j] += dict[k][j*m/num_type];
j++;
}
i += j;
}
num_type /= m;
k++;
}
return ans;
}
};