How can I simplify or clean up this anagram method?

懵懂的女人 提交于 2019-12-01 01:59:30
test_words.group_by{|w| w.each_char.sort}.values

would give

[
  ["cars", "racs", "scar"],
  ["for"],
  ["potatoes"],
  ["four"],
  ["creams", "scream"]
]

I modified sawa's answer slightly in order to ignore case and make sure there's no duplicate values:

test_words.group_by{|w| w.downcase.each_char.sort}.values.each{|v| v.uniq!}

I realize this will still give duplicates in the output if the words have characters with different cases, but that's fine for my purposes. Now I'm all sorted, thanks!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!