-1

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
kanjiowl
  • 13
  • 1
  • What about your efforts? – MBo Jul 14 '18 at 11:34
  • Ignoring repeats, the answer is just the product of lengths of each list. I'll leave working out how to handle repeats up to you (it isn't difficult, and it is appropriate you demonstrate more effort than just posting a copy of your question). – Peter Jul 14 '18 at 11:36
  • That was laziness on my part. I did not want to explain how I came upon the problem. This is an analogous problem. I wanted to know how someone else would solve it. – kanjiowl Jul 14 '18 at 16:32

1 Answers1

1

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392