-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
34 lines (31 loc) · 759 Bytes
/
res.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* res.js
* @authors Joe Jiang ([email protected])
* @date 2017-05-10 22:14:38
*
* @param {string[]} strs
* @return {string[][]}
*/
let groupAnagrams = function(strs) {
let len = strs.length;
if (!len) return [];
let hashmap = new Map();
for (let i = 0; i < len; i++) {
let tmparr = strs[i].split('');
tmparr.sort();
tmparr = tmparr.join();
if (!hashmap.has(tmparr)) {
hashmap.set(tmparr, [strs[i]]);
} else {
let tmp = hashmap.get(tmparr);
tmp.push(strs[i]);
hashmap.set(tmparr, tmp);
}
}
let res = [];
console.log(hashmap);
for (let [key, value] of hashmap) {
res.push(value);
}
return res;
};