Created
November 24, 2021 18:35
-
-
Save ChrisDobby/82c2e15cd4915a0e8a105328d5e16765 to your computer and use it in GitHub Desktop.
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 groupAnagrams(anagrams: string[]) { | |
const grouped: { [name: string]: string[] } = {}; | |
for(const anagram of anagrams) { | |
const sorted = anagram.split('').sort().join(''); | |
if(!grouped[sorted]) { | |
grouped[sorted] = [anagram]; | |
} else { | |
grouped[sorted].push(anagram); | |
} | |
} | |
return Object.values(grouped).sort(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment