refactor(anagrams): improve code legibility
This commit is contained in:
@ -6,11 +6,11 @@ Use `map()` to combine the letter with each partial anagram, then `reduce()` to
|
||||
Base cases are for string `length` equal to `2` or `1`.
|
||||
|
||||
```js
|
||||
const 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;
|
||||
const anagrams = str => {
|
||||
if(str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
|
||||
return str.split('').reduce( (acc, letter, index) => {
|
||||
anagrams(str.slice(0, index) + str.slice(index + 1)).map( value => acc.push(letter + value) );
|
||||
return acc;
|
||||
}, []);
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user