Created
July 29, 2020 03:34
-
-
Save dobleuber/a094cc71a66c772381dd37a6119e2936 to your computer and use it in GitHub Desktop.
Hallar las permutaciones de un 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
const stringPermutations = str => { | |
if (str.length === 1) return [str]; | |
return str | |
.split('') | |
.reduce( | |
(acc, letter, i) => | |
acc.concat( | |
stringPermutations(str.slice(0, i) + str.slice(i + 1) | |
) | |
.map(val => letter + val)) | |
,[] | |
); | |
}; | |
console.log('Result', stringPermutations('abcde')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment