Skip to content

Instantly share code, notes, and snippets.

@mitrakmt
Created September 20, 2016 15:09
Show Gist options
  • Save mitrakmt/91f770699b0ba5639435be7a1a103461 to your computer and use it in GitHub Desktop.
Save mitrakmt/91f770699b0ba5639435be7a1a103461 to your computer and use it in GitHub Desktop.
All permutations of a set interview challenge.
function getAllPermutations(string) {
var results = [];
if (string.length === 1) {
results.push(string);
return results;
}
for (var i = 0; i < string.length; i++) {
var firstChar = string[i];
var charsLeft = string.substring(0, i) + string.substring(i + 1);
var innerPermutations = getAllPermutations(charsLeft);
for (var 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