Skip to content

Commit ed2b293

Browse files
committed
Group Anagrams Solution
1 parent 1d88643 commit ed2b293

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

group-anagrams/naringst.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+
6+
/**
7+
* Runtime: 100ms, Memory: 62.34MB
8+
* n = strs.length
9+
* k = 문자열의 최대 길이
10+
* Time complexity: O(n * k log k)
11+
* -> klogk는 길이가 k인 문자열을 sort
12+
* -> n은 이걸 각 문자열마다 반복하기 때문
13+
*
14+
* Space complexity: O(n)
15+
*
16+
*/
17+
18+
var groupAnagrams = function (strs) {
19+
let answer = new Map();
20+
21+
for (let j = 0; j < strs.length; j++) {
22+
const sortedWord = strs[j].split("").sort().join("");
23+
answer.has(sortedWord)
24+
? answer.set(sortedWord, [...answer.get(sortedWord), strs[j]])
25+
: answer.set(sortedWord, [strs[j]]);
26+
}
27+
28+
return Array.from(answer.values());
29+
};

0 commit comments

Comments
 (0)