I have been working on a problem that can be described as the following.
You are given 3 lists of characters.
L1 = [a,b ,c,d ]
L2 = [e,f ,g ,a]
L3 = [m, n, o, g, k]
How many strings can you make by taking a character from each list?
You are allowed to pick only one character from each list (length of the string should be 3). The resultant string should not have any characters repeating. Each list is guaranteed to be containing unique letters. The order of list you pick from does not matter.
You could create the Cartesian product of multiple arrays and then filter the array by counting unique items in a set.
var data = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'a'], ['m', 'n', 'o', 'g', 'k']],
result = data
.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []))
.filter(a => new Set(a).size === a.length);
console.log(result.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments