Skip to content

Commit 522a525

Browse files
committed
finish 49
1 parent 3bf0577 commit 522a525

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

49. Group Anagrams.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 49. Group Anagrams
3+
*
4+
* Given an array of strings, group anagrams together.
5+
*
6+
* Example:
7+
*
8+
* Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
9+
* Output:
10+
* [
11+
* ["ate","eat","tea"],
12+
* ["nat","tan"],
13+
* ["bat"]
14+
* ]
15+
*
16+
* Note:
17+
*
18+
* All inputs will be in lowercase.
19+
* The order of your output does not matter.
20+
*/
21+
22+
/**
23+
* @param {string[]} strs
24+
* @return {string[][]}
25+
*/
26+
var groupAnagrams = function(strs) {
27+
var res = {};
28+
var str = '';
29+
var len = strs.length;
30+
for (var i = 0; i < len; i++) {
31+
str = Array.from(strs[i]).sort().join('');
32+
if (!res[str]) res[str] = [];
33+
res[str].push(strs[i]);
34+
}
35+
return Object.values(res);
36+
};
37+
38+
// 把每个字符串排序一下,用哈希表存起来

0 commit comments

Comments
 (0)