Find out whether or not 2 words are anagrams by using the fundamental theorem of arithmetic
// SNIPPET
// Quick test inspired by @fermatslibrary's algorithm idea (https://twitter.com/fermatslibrary/status/988399621402656773)
//: - Dictionary with a prime number assigned to each character
var primesOfChars: [String: Int] = ["a":2, "b":3, "c":5, "d":7, "e":11, "f":13, "g":17, "h":19, "i":23, "j":29, "k":31, "l":37, "m":41, "n":43, "o":47, "p":53,"q":59, "r":61, "s":67, "t":71,"u":73, "v":79, "w":83, "x":89, "y":97, "z":101]
//: - Fundamental theorem of arithmetic
func arithmetic(word: String) -> Int {
var product = 1
for char in word {
product = product * primeOfChars["\(char)"]!
}
return product
}
//: - Compare **unique** prime products
func anagram(string1: String, string2: String) -> Bool {
return arithmetic(word: string1.lowercased()) == arithmetic(word: string2.lowercased())
}
anagram(string1: "lukas", string2: "sulak") // true
anagram(string1: "elon", string2: "tony") // false