Skip to content

Commit

Permalink
Create 242-Valid-Anagrams.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
chandra9302 authored Apr 5, 2022
1 parent 933ca4a commit af2a864
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions swift/242-Valid-Anagrams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Question Link: https://leetcode.com/problems/valid-anagram/
*/

class ValidAnagram {
func isAnagram(_ s: String, _ t: String) -> Bool {
if s.count != t.count {
return false
}
var countS = [Int:Int]()
var countT = [Int:Int]()
let sChars = Array(s)
let tChars = Array(t)
for i in 0..<sChars.count {
countS[Int(sChars[i].asciiValue!)] = 1 + countS[Int(sChars[i].asciiValue!), default: 0]
countT[Int(tChars[i].asciiValue!)] = 1 + countT[Int(tChars[i].asciiValue!), default: 0]
}
for (key, _) in countS {
if countS[key] != countT[key, default: 0] {
return false
}
}
return true
}
}

0 comments on commit af2a864

Please sign in to comment.