Skip to content

Commit facd449

Browse files
committed
Group Anagrams
1 parent af47da3 commit facd449

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

group-anagrams/hyejjun.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function (strs) {
6+
let map = new Map();
7+
8+
for (let str of strs) {
9+
let sortedStr = str.split('').sort().join('');
10+
11+
if (map.has(sortedStr)) {
12+
map.get(sortedStr).push(str);
13+
} else {
14+
map.set(sortedStr, [str]);
15+
}
16+
}
17+
18+
return Array.from(map.values());
19+
};
20+
21+
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
22+
console.log(groupAnagrams([""]));
23+
console.log(groupAnagrams(["a"]));
24+
25+
26+
/*
27+
시간 복잡도: O(n*k log k)
28+
공간 복잡도: O(n*k)
29+
*/

0 commit comments

Comments
 (0)