SHARE
TWEET

Untitled

a guest Jul 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = function getCombinations(letters, count, shift = 0) {
  2.   let variants = [];
  3.   for (let i = shift; i < letters.length; i++) {
  4.     if (count > 1) {
  5.       const childCombinations = getCombinations(letters, count - 1, i + 1);
  6.       for (let c = 0; c < childCombinations.length; c++) {
  7.         variants.push([letters[i]].concat(childCombinations[c]));
  8.       }
  9.     } else {
  10.       variants.push([letters[i]])
  11.     }
  12.   }
  13.  
  14.   return variants;
  15. }
  16.  
  17. /*
  18. Example:
  19. getCombinations('abc', 2);
  20.  
  21. Result: [ [ 'a', 'b' ], [ 'a', 'c' ], [ 'b', 'c' ] ]
  22. // 5 ms
  23. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top