Skip to content

Commit bcc8b4b

Browse files
authored
feat: add swift implementation to lcci problem: No.10.02 (#2712)
1 parent 911f098 commit bcc8b4b

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Diff for: lcci/10.02.Group Anagrams/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ function groupAnagrams(strs: string[]): string[][] {
120120
}
121121
```
122122

123+
```swift
124+
class Solution {
125+
func groupAnagrams(_ strs: [String]) -> [[String]] {
126+
var d = [String: [String]]()
127+
for s in strs {
128+
let t = String(s.sorted())
129+
d[t, default: []].append(s)
130+
}
131+
return Array(d.values)
132+
}
133+
}
134+
```
135+
123136
<!-- tabs:end -->
124137

125138
### 方法二:计数

Diff for: lcci/10.02.Group Anagrams/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ function groupAnagrams(strs: string[]): string[][] {
127127
}
128128
```
129129

130+
```swift
131+
class Solution {
132+
func groupAnagrams(_ strs: [String]) -> [[String]] {
133+
var d = [String: [String]]()
134+
for s in strs {
135+
let t = String(s.sorted())
136+
d[t, default: []].append(s)
137+
}
138+
return Array(d.values)
139+
}
140+
}
141+
```
142+
130143
<!-- tabs:end -->
131144

132145
### Solution 2: Counting

Diff for: lcci/10.02.Group Anagrams/Solution.swift

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
func groupAnagrams(_ strs: [String]) -> [[String]] {
3+
var d = [String: [String]]()
4+
for s in strs {
5+
let t = String(s.sorted())
6+
d[t, default: []].append(s)
7+
}
8+
return Array(d.values)
9+
}
10+
}

0 commit comments

Comments
 (0)