Skip to content

Commit 4844d5d

Browse files
committed
group-anagrams solution
1 parent 448f741 commit 4844d5d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

group-anagrams/krokerdile.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function(strs) {
6+
const dict = new Map();
7+
8+
strs.forEach(str => {
9+
const sorted = str.split('').sort().join('');
10+
if (!dict.has(sorted)) {
11+
dict.set(sorted, [str]);
12+
} else {
13+
dict.get(sorted).push(str);
14+
}
15+
});
16+
17+
// value 길이 기준 내림차순 정렬
18+
const result = [...dict.entries()]
19+
.sort((a, b) => b[1].length - a[1].length)
20+
.map(([_, group]) => group); // value (즉, 아나그램 그룹)만 꺼냄
21+
22+
return result;
23+
};

0 commit comments

Comments
 (0)