Anagrams group algorithm in Ruby