Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created February 20, 2014 21:41
Show Gist options
  • Save mayfer/9123824 to your computer and use it in GitHub Desktop.
Save mayfer/9123824 to your computer and use it in GitHub Desktop.
Find the word with most number of anagrams
words = File.open('/usr/share/dict/words').read
words.gsub!(/\r\n?/, "")
hashes = {}
anagrams = {}
most = 1
most_anagram = ''
words.each_line do |word|
word = word.strip.downcase
sorted = word.chars.sort.join('')
if hashes[sorted] == nil
hashes[sorted] = 0
end
hashes[sorted] += 1
if hashes[sorted] >= most
most = hashes[sorted]
most_anagram = sorted
print most, " ", word
puts
end
end
puts
words.each_line do |word|
word = word.strip.downcase
sorted = word.chars.sort.join('')
if sorted == most_anagram
print word
puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment