LeeCode-刷题日记-Day21

49. 字母异位词分组

思路:异位词中的每个字符出现次数相同。将字符出现次数存入数组中。用次数数组作为key和字符串作为value建立对应关系,如果次数相同则加入对应的value数组。不同则创建新的key-value关系

/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function(strs) {
    const map = new Object();
    for(let s of strs){
        const count= new Array(26).fill(0);
        for(let c of s){
            count[c.charCodeAt()-'a'.charCodeAt()]++;
        }
        map[count]?map[count].push(s):map[count]=[s];
    }
    return Object.values(map);
};