Created
May 24, 2012 15:15
-
-
Save bjjb/2782162 to your computer and use it in GitHub Desktop.
Anagram Counter!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AnagramCounter | |
def count_anagrams | |
Hash[group_by { |x| x.split('').sort.join }.map { |x, y| [x, y.length] }] | |
end | |
end | |
Array.send(:include, AnagramCounter) | |
puts ["abb", "bba", "aba", "bba", "abb", "aab", "abc"].count_anagrams.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was asked to make this in a phone interview a few minutes ago, and got totally flustered! I hate being put on the spot. Here's what I SHOULD have done.