Created
January 8, 2019 07:41
-
-
Save kevinahn7/f6bb492c36a0d79b98945bb4681b1171 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) { | |
| 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