Group the strings that are anagrams of each other. The groups may be returned in any order.
Anagrams also share the same 26 letter counts. Encoding the counts as a key avoids sorting each word.
1function groupAnagrams(strs: string[]): string[][] {2const groups = new Map<string, string[]>();3for (const w of strs) {4const cnt = new Array(26).fill(0);5for (const ch of w) cnt[ch.charCodeAt(0) - 97]++;6const key = cnt.join("#");7if (!groups.has(key)) groups.set(key, []);8groups.get(key)!.push(w);9}10return [...groups.values()];11}
Empty map.
Space: play/pause · ←/→: step