Swap, anagrams, occurences

Fixed sorting title for strings.
This commit is contained in:
Angelos Chalaris
2017-12-06 23:50:23 +02:00
parent e86bf2ffa3
commit f40115ff3b
5 changed files with 67 additions and 1 deletions

View File

@ -0,0 +1,16 @@
### Anagrams of string (with duplicates)
Use recursion.
For each letter in the given string, create all the partial anagrams for the rest of its letters.
Use `map()` to combine the letter with each partial anagram, then `reduce()` to combine all anagrams in one array.
Base cases are for string `length` equal to `2` or `1`.
```js
var anagrams = s => {
if(s.length <= 2) return s.length === 2 ? [s, s[1] + s[0]] : [s];
return s.split('').reduce( (a,l,i) => {
anagrams(s.slice(0,i) + s.slice(i+1)).map( v => a.push(l+v) );
return a;
}, []);
}
```