Skip to content

Instantly share code, notes, and snippets.

@bjjb
Created May 24, 2012 15:15
Show Gist options
  • Save bjjb/2782162 to your computer and use it in GitHub Desktop.
Save bjjb/2782162 to your computer and use it in GitHub Desktop.
Anagram Counter!
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
@bjjb
Copy link
Author

bjjb commented May 24, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment