Skip to content

Commit af2a864

Browse files
authored
Create 242-Valid-Anagrams.swift
1 parent 933ca4a commit af2a864

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

swift/242-Valid-Anagrams.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/valid-anagram/
3+
*/
4+
5+
class ValidAnagram {
6+
func isAnagram(_ s: String, _ t: String) -> Bool {
7+
if s.count != t.count {
8+
return false
9+
}
10+
var countS = [Int:Int]()
11+
var countT = [Int:Int]()
12+
let sChars = Array(s)
13+
let tChars = Array(t)
14+
for i in 0..<sChars.count {
15+
countS[Int(sChars[i].asciiValue!)] = 1 + countS[Int(sChars[i].asciiValue!), default: 0]
16+
countT[Int(tChars[i].asciiValue!)] = 1 + countT[Int(tChars[i].asciiValue!), default: 0]
17+
}
18+
for (key, _) in countS {
19+
if countS[key] != countT[key, default: 0] {
20+
return false
21+
}
22+
}
23+
return true
24+
}
25+
}

0 commit comments

Comments
 (0)