您的位置:首页 > 其它

How to group anagrams in a string into an array

2014-09-25 15:05 671 查看
I want to group anagrams in ruby from a given string and the result should be an array containing arrays of anagram groups like
Given String: "scream cars for four scar creams"
here cars and scar are anagrams(made by same characters)
and the result should be [[cars, scar], ['for'], ['four'], ['creams', 'scream']]

"scream cars for four scar creams".split.group_by{|w| w.chars.sort}.values
#=> [["scream", "creams"], ["cars", "scar"], ["for"], ["four"]]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: