Skip to content

Instantly share code, notes, and snippets.

@csemresari
Last active June 9, 2020 02:08
Show Gist options
  • Save csemresari/0df1f08d15d259d0c1c184e258e6bc65 to your computer and use it in GitHub Desktop.
Save csemresari/0df1f08d15d259d0c1c184e258e6bc65 to your computer and use it in GitHub Desktop.
// Checks if a string is anagram with another
extension String {
func isAnagramWith(s: String) -> Bool {
var dict = [Character:Int]()
for char in self{
dict[char] = 1 + (dict[char] ?? 0)
}
for char in s {
if dict[char] != nil {
let val = dict[char]! - 1
dict[char] = val == 0 ? nil : val
}else{
return false
}
}
return dict.count == 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment