Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = function getCombinations(letters, count, shift = 0) {
- let variants = [];
- for (let i = shift; i < letters.length; i++) {
- if (count > 1) {
- const childCombinations = getCombinations(letters, count - 1, i + 1);
- for (let c = 0; c < childCombinations.length; c++) {
- variants.push([letters[i]].concat(childCombinations[c]));
- }
- } else {
- variants.push([letters[i]])
- }
- }
- return variants;
- }
- /*
- Example:
- getCombinations('abc', 2);
- Result: [ [ 'a', 'b' ], [ 'a', 'c' ], [ 'b', 'c' ] ]
- // 5 ms
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement