You are viewing a single comment's thread. Return to all comments →
Ruby solution:
def sherlockAndAnagrams(s) substrings = Hash.new { |h,k| h[k] = 0 } (0..s.length - 1).each do |i| (i..s.length - 1).each do |j| sorted = s[i..j].chars.sort.join substrings[sorted] += 1 end end substrings.values. filter { |f| f > 1 }. map { |k| k * (k - 1) / 2 }. sum 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 →
Ruby solution: