Skip to content

Instantly share code, notes, and snippets.

@kevinahn7
Created January 8, 2019 07:41
Show Gist options
  • Select an option

  • Save kevinahn7/f6bb492c36a0d79b98945bb4681b1171 to your computer and use it in GitHub Desktop.

Select an option

Save kevinahn7/f6bb492c36a0d79b98945bb4681b1171 to your computer and use it in GitHub Desktop.
function getAllPermutations(string) {
let results = [];
if (string.length === 1) {
results.push(string);
return results;
}
for (let i = 0; i < string.length; i++) {
let firstChar = string[i];
let charsLeft = string.substring(0, i) + string.substring(i + 1);
let innerPermutations = getAllPermutations(charsLeft);
for (let j = 0; j < innerPermutations.length; j++) {
results.push(firstChar + innerPermutations[j]);
}
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment