Skip to content

Instantly share code, notes, and snippets.

@PantheraRed
Last active April 26, 2022 19:55
Show Gist options
  • Save PantheraRed/abd75665da3e62f902e3f5615b1bc9ed to your computer and use it in GitHub Desktop.
Save PantheraRed/abd75665da3e62f902e3f5615b1bc9ed to your computer and use it in GitHub Desktop.
Find all the possible combinations of a string.
'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;
}
import combinations from "./combinations.js";
combinations("abcdef"); // [ 'abcdef', 'bcdefa', 'cdefab', 'defabc', 'efabcd', 'fabcde' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment