Skip to content

Commit 5136f25

Browse files
authored
feat: add swift implementation to lcof2 problem: No.032 (doocs#3037)
1 parent df717f7 commit 5136f25

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lcof2/剑指 Offer II 032. 有效的变位词/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,35 @@ function isAnagram(s: string, t: string): boolean {
170170
}
171171
```
172172

173+
#### Swift
174+
175+
```swift
176+
class Solution {
177+
func isAnagram(_ s: String, _ t: String) -> Bool {
178+
let m = s.count
179+
let n = t.count
180+
if m != n || s == t {
181+
return false
182+
}
183+
184+
var cnt = [Int](repeating: 0, count: 26)
185+
186+
for (sc, tc) in zip(s, t) {
187+
cnt[Int(sc.asciiValue! - Character("a").asciiValue!)] += 1
188+
cnt[Int(tc.asciiValue! - Character("a").asciiValue!)] -= 1
189+
}
190+
191+
for x in cnt {
192+
if x != 0 {
193+
return false
194+
}
195+
}
196+
197+
return true
198+
}
199+
}
200+
```
201+
173202
<!-- tabs:end -->
174203

175204
<!-- solution:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func isAnagram(_ s: String, _ t: String) -> Bool {
3+
let m = s.count
4+
let n = t.count
5+
if m != n || s == t {
6+
return false
7+
}
8+
9+
var cnt = [Int](repeating: 0, count: 26)
10+
11+
for (sc, tc) in zip(s, t) {
12+
cnt[Int(sc.asciiValue! - Character("a").asciiValue!)] += 1
13+
cnt[Int(tc.asciiValue! - Character("a").asciiValue!)] -= 1
14+
}
15+
16+
for x in cnt {
17+
if x != 0 {
18+
return false
19+
}
20+
}
21+
22+
return true
23+
}
24+
}

0 commit comments

Comments
 (0)