Created
January 28, 2019 22:30
-
-
Save yammik/ebeb878908391cc957fcc63594e9638d to your computer and use it in GitHub Desktop.
Creating permutations of a string
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 perm(str) { | |
const results = []; | |
if (str.length === 1) return str; | |
for (let i = 0; i < str.length; i++) { | |
const currentChar = str[i]; | |
const remainingChars = str.slice(0, i) + str.slice(i+1); | |
const innerPermutations = perm(remainingChars); | |
for (let j = 0; j < innerPermutations.length; j++) { | |
results.push(currentChar + innerPermutations[j]); | |
} | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment