Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Arvin iuunhao
Added: Jun 18, 2018 8:31 AM
Views: 8
Tags: js
字符串的排列组合,带有重复项
  1. const anagrams = str => {
  2.   if (str.length < = 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
  3.   return str.split('').reduce((acc, letter, i) =>
  4.     acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)), []);
  5. };
  6. // anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']