You are viewing a single comment's thread. Return to all comments →
def sherlockAndAnagrams(str) counter = Hash.new(0) (1...str.length).each do |len| str.chars.each_cons(len) do |substr| counter[substr.sort.join] += 1 end end counter.values.map { |n| n * (n - 1) / 2 }.reduce(:+) end
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 →