Skip to content

Commit b71a31d

Browse files
committed
Add group anagrams solution
1 parent 9100c95 commit b71a31d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

group-anagrams/hyunjung-choi.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* N: 단어 개수, M: 평균 단어 길이
3+
* 시간 복잡도: O(N × M log M)
4+
* 공간 복잡도: O(N × M)
5+
*/
6+
7+
class Solution {
8+
fun groupAnagrams(strs: Array<String>): List<List<String>> {
9+
val groupMap = mutableMapOf<String, MutableList<String>>()
10+
11+
for (str in strs) {
12+
val sortedKey = str.toCharArray().sorted().joinToString("")
13+
groupMap.getOrPut(sortedKey) { mutableListOf()}.add(str)
14+
}
15+
16+
return groupMap.values.toList()
17+
}
18+
}

0 commit comments

Comments
 (0)