Created
August 13, 2018 23:58
-
-
Save minmaxdata/465db661065a0f11c0568bc6f19a137f to your computer and use it in GitHub Desktop.
anagrams? created by minmaxdata - https://repl.it/@minmaxdata/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
| function anagram(first, second) { | |
| let one = [...first]; | |
| let two = [...second]; | |
| let temp = {} | |
| const len = one.length === two.length; | |
| if (len) { | |
| temp = one.reduce(function (letters, letter) { | |
| if (letter in letters) { | |
| letters[letter]++ | |
| } else { | |
| letters[letter] = 1 | |
| } | |
| return letters | |
| }, {}); | |
| two.map(alpha => { | |
| if (temp[alpha] > 0) { | |
| temp[alpha]--; | |
| } | |
| if (temp[alpha] === 0) { | |
| delete temp[alpha]; | |
| } | |
| }) | |
| } | |
| return Object.keys(temp).length === 0; | |
| } | |
| console.log('anagram', anagram('restful', 'fluster')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment