Skip to content

Commit c1b622b

Browse files
committed
group anagrams
1 parent 14eecaf commit c1b622b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

group-anagrams/sonjh1217.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
func groupAnagrams(_ strs: [String]) -> [[String]] {
3+
var stringsByCount = [[Int]: [String]]()
4+
5+
strs.map { str in
6+
var countsByAlphabet = Array(repeating: 0, count: 26)
7+
for char in str.unicodeScalars {
8+
countsByAlphabet[Int(char.value) - 97] += 1
9+
}
10+
stringsByCount[countsByAlphabet, default: []].append(str)
11+
}
12+
13+
return Array(stringsByCount.values)
14+
}
15+
//시간 O(n*L) L=string들의 평균 길이
16+
//공간 O(n*L)
17+
}
18+

0 commit comments

Comments
 (0)