You are viewing a single comment's thread. Return to all comments →
I've built a code in Swift based on your algorithm and that worked very well. Thanks! Here's the code:
func sherlockAndAnagrams(s: String) -> Int { let chars = Array(s) var nAnagrams = 0 for length in 0...chars.count-2 { var counter:[String:Int] = [:] for i in 0..<chars.count - length { let txt = String(chars[i...i+length].sorted()) counter[txt] = (counter[txt] ?? 0) + 1 } for c in counter { nAnagrams += c.value * (c.value - 1)/2 } } return nAnagrams }
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
I've built a code in Swift based on your algorithm and that worked very well. Thanks! Here's the code: