import combinations from "./combinations.js";
combinations("abcdef"); // [ 'abcdef', 'bcdefa', 'cdefab', 'defabc', 'efabcd', 'fabcde' ]
Last active
April 26, 2022 19:55
-
-
Save PantheraRed/abd75665da3e62f902e3f5615b1bc9ed to your computer and use it in GitHub Desktop.
Find all the possible combinations of a string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' // Optional | |
export default function combinations(combos) { | |
if (!Array.isArray(combos)) { | |
combos = [combos]; | |
} | |
const a = combos[combos.length - 1].split(''); | |
let b = a.shift(); | |
a.push(b); | |
b = a.join(''); | |
if (!combos.includes(b)) { | |
combos.push(b); | |
return combinations(combos); | |
} | |
return combos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment