-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBrace Expansion II.js
46 lines (40 loc) · 1.12 KB
/
Brace Expansion II.js
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
var braceExpansionII = function(expression) { // , mutiplier = ['']
const char = ('{' + expression + '}').split('').values()
const result = [...rc(char)];
result.sort((a,b) => a.localeCompare(b));
return result;
};
function rc (char) {
const result = new Set();
let resolved = ['']
let chars = '';
let currentChar = char.next().value;
while (currentChar !== '}' && currentChar) {
if (currentChar === '{') {
resolved = mix(mix(resolved, [chars]), rc(char));
chars = '';
} else if (currentChar === ',') {
for (const v of mix(resolved, [chars])) {
result.add(v);
}
chars = '';
resolved = [''];
} else {
chars += currentChar;
}
currentChar = char.next().value;
}
for (const v of mix(resolved, [chars])) {
result.add(v);
}
return result; // everything
}
function mix (a, b) {
const result = [];
for (const ca of a) {
for (const cb of b) {
result.push(ca + cb);
}
}
return result;
}