Skip to content

Commit a7fec99

Browse files
committed
feat: group anagrams
1 parent f5afe89 commit a7fec99

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

group-anagrams/anniemon.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 시간 복잡도:
3+
* 정렬 작업은 각 문자열의 길이가 m일 때 O(m logm)이고, 총 strs의 길이만큼 수행되므로
4+
* 시간 복잡도는 O(n * mlogm)
5+
* 공간 복잡도:
6+
* Map 키는 최대 길이 m인 문자열 strs.length개이다.
7+
* 따라서 공간 복잡도는 O(n * m)
8+
*/
9+
/**
10+
* @param {string[]} strs
11+
* @return {string[][]}
12+
*/
13+
var groupAnagrams = function(strs) {
14+
const map = new Map();
15+
for(const s of strs) {
16+
const key = s.split('').sort().join('');
17+
if(!map.has(key)) {
18+
map.set(key, [])
19+
}
20+
map.get(key).push(s);
21+
}
22+
return Array.from(map.values());
23+
};

0 commit comments

Comments
 (0)