Skip to content

Instantly share code, notes, and snippets.

@leafiy
Created May 18, 2018 18:56
Show Gist options
  • Select an option

  • Save leafiy/ab66ca814d58da2410d115cc16c77569 to your computer and use it in GitHub Desktop.

Select an option

Save leafiy/ab66ca814d58da2410d115cc16c77569 to your computer and use it in GitHub Desktop.
anagrams
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