Guest User

Untitled

a guest
May 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. func dictSignature(s : String) -> Dictionary<String, Int> {
  2. var chars = Dictionary<String, Int>();
  3. for y in s {
  4. var x = String(y)
  5. if let v = chars[x] {
  6. chars.updateValue(v + 1, forKey: x)
  7. chars[x] = v + 1;
  8. } else {
  9. chars[x] = 1;
  10. }
  11. }
  12. return chars;
  13. }
  14.  
  15. func isAnagram(s1: String, s2: String) -> Bool {
  16. let (d1, d2) = (dictSignature(s1), dictSignature(s2));
  17. return d1 == d2;
  18. }
  19.  
  20. let y = isAnagram("testing", "one two three");
  21. let x = isAnagram("testing", "gnitset");
Add Comment
Please, Sign In to add comment