Created
May 18, 2018 18:56
-
-
Save leafiy/ab66ca814d58da2410d115cc16c77569 to your computer and use it in GitHub Desktop.
anagrams
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 anagrams = str => { | |
| if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str]; | |
| return str.split('').reduce((acc, letter, i) => | |
| acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)), []); | |
| }; | |
| // anagrams('abc') -> ['abc','acb','bac','bca','cab','cba'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment