Last active
June 9, 2020 02:08
-
-
Save csemresari/0df1f08d15d259d0c1c184e258e6bc65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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