Last active
October 11, 2018 22:53
-
-
Save lienista/6c7f30d2713f290ff86f092e92612b67 to your computer and use it in GitHub Desktop.
(Algorithms in Javascript) Leetcode 49. Group Anagrams - Given an array of strings, group anagrams together.
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
| const groupAnagrams => (strs) => { | |
| let charMap = {}; | |
| let anagram = (element, index) => { | |
| let thisKey = element.split('').sort().join(''); | |
| if(charMap.hasOwnProperty(thisKey)){ | |
| charMap[thisKey].unshift(element); | |
| } else { | |
| charMap[thisKey] = [element]; | |
| } | |
| } | |
| strs.forEach(anagram); | |
| return Object.values(charMap); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment