#20
Hashmap Grouping
HashmapGroup a list of words so that anagrams of each other are in the same group.
function groupAnagrams(strs: string[]): string[][] {
const groups = new Map<string, string[]>();
for (const word of strs) {
const key = [...word].sort().join(''); // canonical form = sorted letters
const bucket = groups.get(key);
if (bucket) bucket.push(word);
else groups.set(key, [word]);
}
return [...groups.values()];
}def group_anagrams(strs)
strs.group_by { |word| word.chars.sort.join }.values # canonical key = sorted letters
end