File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed
lcof2/剑指 Offer II 032. 有效的变位词 Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,35 @@ function isAnagram(s: string, t: string): boolean {
170
170
}
171
171
```
172
172
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
+
173
202
<!-- tabs: end -->
174
203
175
204
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments