Skip to content

Commit 04a1960

Browse files
authored
[ PS ] : Group Anagrams
1 parent dc5bd6f commit 04a1960

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

โ€Žgroup-anagrams/uraflower.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* ์• ๋„ˆ๊ทธ๋žจ๋ผ๋ฆฌ ๋ฌถ์–ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {string[]} strs
4+
* @return {string[][]}
5+
*/
6+
const groupAnagrams = function(strs) {
7+
// ํ’€์ด 1
8+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n*s) (n: strs.length, s: str.length)
9+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
10+
function groupManually () {
11+
const groups = {};
12+
13+
strs.forEach((str) => {
14+
const key = [...str].sort().join('');
15+
if (!groups[key]) groups[key] = [];
16+
groups[key].push(str);
17+
});
18+
19+
return Object.values(groups);
20+
}
21+
22+
// ํ’€์ด 2
23+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n*s) (n: strs.length, s: str.length)
24+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
25+
function groupByAnagram() {
26+
const result = Object.groupBy(strs, (str) => [...str].sort().join(''));
27+
return Object.values(result);
28+
}
29+
30+
return groupByAnagram();
31+
};
32+

0 commit comments

Comments
ย (0)