Open
Description
index.ts
export function groupAnagrams(strs: string[]): string[][] {
const anagramMap = new Map<string, string[]>();
for (let i = 0; i < strs.length; i++) {
const current = strs[i];
const sortedStr = current.split("").sort().join();
if (anagramMap.has(sortedStr)) {
anagramMap.get(sortedStr)!.push(current);
} else {
anagramMap.set(sortedStr, [current]);
}
}
return Array.from(anagramMap.values());
}