Brace Expansion II LeetCode solution in JavaScript

1
var braceExpansionII = function (expression) {
2
// , mutiplier = ['']
3
const char = ("{" + expression + "}").split("").values();
4
const result = [...rc(char)];
5
result.sort((a, b) => a.localeCompare(b));
6
return result;
7
};
8

9
function rc(char) {
10
const result = new Set();
11

12
let resolved = [""];
13
let chars = "";
14
let currentChar = char.next().value;
15

16
while (currentChar !== "}" && currentChar) {
17
if (currentChar === "{") {
18
resolved = mix(mix(resolved, [chars]), rc(char));
19
chars = "";
20
} else if (currentChar === ",") {
21
for (const v of mix(resolved, [chars])) {
22
result.add(v);
23
}
24
chars = "";
25
resolved = [""];
26
} else {
27
chars += currentChar;
28
}
29
currentChar = char.next().value;
30
}
31

32
for (const v of mix(resolved, [chars])) {
33
result.add(v);
34
}
35

36
return result; // everything
37
}
38

39
function mix(a, b) {
40
const result = [];
41
for (const ca of a) {
42
for (const cb of b) {
43
result.push(ca + cb);
44
}
45
}
46
return result;
47
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0