Skip to content

Commit 27ef428

Browse files
committed
add: group-anagrams
1 parent ebda7ae commit 27ef428

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n * m log m)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
3+
4+
// HashMap ์‚ฌ์šฉ
5+
// ๊ฐ ๋ฌธ์ž์—ด์„ ์ •๋ ฌํ•˜์—ฌ ํ‚ค๋กœ ์‚ฌ์šฉ
6+
// ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฃนํ™”
7+
8+
/**
9+
* @param {string[]} strs
10+
* @return {string[][]}
11+
*/
12+
var groupAnagrams = function (strs) {
13+
const map = {};
14+
15+
for (const str of strs) {
16+
const key = str.split('').sort().join('');
17+
if (!map[key]) {
18+
map[key] = [];
19+
}
20+
map[key].push(str);
21+
}
22+
23+
return Object.values(map);
24+
};

0 commit comments

Comments
ย (0)