Created
September 20, 2016 15:09
-
-
Save mitrakmt/91f770699b0ba5639435be7a1a103461 to your computer and use it in GitHub Desktop.
All permutations of a set interview challenge.
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
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