Skip to content

Instantly share code, notes, and snippets.

@amejiarosario
Created January 13, 2020 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amejiarosario/bc62decc4b333f612740df14deca8882 to your computer and use it in GitHub Desktop.
Save amejiarosario/bc62decc4b333f612740df14deca8882 to your computer and use it in GitHub Desktop.
function getPermutations(string, prefix = '') {
if(string.length <= 1) {
return [prefix + string];
}
return Array.from(string).reduce((result, char, index) => {
const reminder = string.slice(0, index) + string.slice(index+1);
result = result.concat(getPermutations(reminder, prefix + char));
return result;
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment