Skip to content

Commit 145b988

Browse files
committed
add: Group Anagrams
1 parent bb0da02 commit 145b988

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
2929
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [JavaScript](./src/multiply-strings/res.js)|Medium|
3030
|46|[Permutations](https://leetcode.com/problems/permutations/) | [JavaScript](./src/permutations/res.js)|Medium|
3131
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/) | [JavaScript](./src/rotate-image/res.js)|Medium|
32+
|49|[Group Anagrams](https://leetcode.com/problems/anagrams/) | [JavaScript](./src/anagrams/res.js)|Medium|
3233
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [JavaScript](./src/spiral-matrix/res.js)|Medium|
3334
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
3435
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js)|Easy|

src/anagrams/res.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017-05-10 22:14:38
5+
*
6+
* @param {string[]} strs
7+
* @return {string[][]}
8+
*/
9+
let groupAnagrams = function(strs) {
10+
let len = strs.length;
11+
if (!len) return [];
12+
13+
let hashmap = new Map();
14+
for (let i = 0; i < len; i++) {
15+
let tmparr = strs[i].split('');
16+
tmparr.sort();
17+
tmparr = tmparr.join();
18+
if (!hashmap.has(tmparr)) {
19+
hashmap.set(tmparr, [strs[i]]);
20+
} else {
21+
let tmp = hashmap.get(tmparr);
22+
tmp.push(strs[i]);
23+
hashmap.set(tmparr, tmp);
24+
}
25+
}
26+
27+
let res = [];
28+
console.log(hashmap);
29+
for (let [key, value] of hashmap) {
30+
res.push(value);
31+
}
32+
33+
return res;
34+
};

0 commit comments

Comments
 (0)