Skip to content

Commit b20c801

Browse files
committed
solve problem
1 parent 5cee6c4 commit b20c801

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

group-anagrams/delight010.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func groupAnagrams(_ strs: [String]) -> [[String]] {
3+
var dict: [[Character: Int]: [String]] = [:]
4+
for str in strs {
5+
var strDict: [Character: Int] = [:]
6+
for char in str {
7+
if let charCount = strDict[char] {
8+
strDict[char] = charCount + 1
9+
} else {
10+
strDict[char] = 1
11+
}
12+
}
13+
if dict[strDict] != nil {
14+
dict[strDict]?.append(str)
15+
} else {
16+
dict[strDict] = [str]
17+
}
18+
}
19+
20+
return dict.values.map { $0 }
21+
}
22+
}
23+

0 commit comments

Comments
 (0)