We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9b971eb commit d184befCopy full SHA for d184bef
group-anagrams/EcoFriendlyAppleSu.kt
@@ -0,0 +1,19 @@
1
+package leetcode_study
2
+
3
+/*
4
+* 주어진 문자열 배열에서 anagram을 그룹핑하는 문제
5
+* 자료구조 Map을 사용해 문제 해결. 오름차순으로 정렬한 char type을 재조합해 key 값으로 사용. value는 조회 대상 문자열
6
+* 시간 복잡도: O(n)
7
+* 공간 복잡도: O(n)
8
+* */
9
+fun groupAnagrams(strs: Array<String>): List<List<String>> {
10
+ val result = mutableMapOf<String, MutableList<String>>()
11
12
+ for (str in strs) {
13
+ val key = str.toCharArray().sorted().joinToString("")
14
+ result.computeIfAbsent(key) { mutableListOf() }.add(str)
15
+ }
16
17
+ if (strs.isEmpty()) return listOf(listOf())
18
+ return result.values.toList()
19
+}
0 commit comments