Skip to content

Commit 0267754

Browse files
committed
feat: solve new question
1 parent f54ec65 commit 0267754

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

group-anagrams/main.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
/*
6+
strs = ["eat","tea","tan","ate","nat","bat"]
7+
*/
8+
9+
var groupAnagrams = function(strs) {
10+
let map = {};
11+
12+
for (let i in strs) {
13+
let sorted = strs[i].split('').sort().join('');
14+
if (!map[sorted]) {
15+
map[sorted] = [strs[i]];
16+
} else {
17+
map[sorted].push(strs[i]);
18+
}
19+
}
20+
return Object.values(map);
21+
};

0 commit comments

Comments
 (0)