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']]
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"]]
相关文章推荐
- how to remove nil and blank string in an array in Ruby
- How to sort an NSMutableArray with custom objects in it?
- Insight into DOMDocument - how to convert data from XML to array in PHP
- How to copy values of an array into another array
- How to Insert Values into an Identity Column in SQL Server
- How to remove the duplicated value in a String array
- In Java, how do I read/convert an InputStream to a String? - Stack Overflow
- How to get array from string contained identical symbols in Ruby?
- How to let TVirtualStringTree to display an icon in disabled state?
- Matching Portions of A String to Elements of an Array in Ruby
- How to split a string to array in objective-c?
- How to Check if an Array Contains a Value in Java Efficiently?---reference
- How to convert char array to String in Java
- How to sum values in an array with different hash
- How to define an array in c#.
- How to sort an array in Ruby to a particular order?
- How to get URL and QueryString value in an ASP.NET page
- How to sort an array of hashes into hashes with multiple values for a key?
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- How to get array from string contained identical symbols in Ruby?