Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  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. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement