需要用到素数表,我们需要将字母转换成素数
var prime = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,
53,59,61,67,71,73,79,83, 89,97,101];
var groupAnagrams = function(strs) {
var hash = {}, ret = [], index = 0;
for(let word of strs){
var sum = 1;
for(let c of word){
var i = c.charCodeAt(0) - 97;
sum *= prime[ i ]
}
if( sum in hash){
ret [ hash[sum] ].push(word)
}else{
hash[sum] = index++
ret[ hash[sum] ] = [word]
}
}
return ret;
};