Skip to content

Commit 63dfef2

Browse files
Merge pull request neetcode-gh#59 from chandra9302/patch-6
Create 242-Valid-Anagrams.swift
2 parents 8fcd768 + a16a7bc commit 63dfef2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

swift/242-Valid-Anagram.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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy