Skip to content

Latest commit

 

History

History
20 lines (16 loc) · 504 Bytes

49. 字母异位词分组.md

File metadata and controls

20 lines (16 loc) · 504 Bytes
  • 哈希表
function groupAnagrams(strs: string[]): string[][] {
    const map: Map<string, string[]> = new Map<string, string[]>();
    for (const str of strs) {
        const key: string = hash(str),
            values: string[] = [...(map.get(key) || []), str];
        map.set(key, values);
    }
    return [...map.values()];
};

function hash(str: string): string {
    return [...str].sort().join('');
}