Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created October 22, 2014 19:03
Show Gist options
  • Save mayfer/1afd4eb72e0c7059f54a to your computer and use it in GitHub Desktop.
Save mayfer/1afd4eb72e0c7059f54a to your computer and use it in GitHub Desktop.
Find the word with most anagrams
# pat, tap, apt
def find_longest_anagram()
words = File.open('/usr/share/dict/words').read
anagrams = Hash.new { [] }
most_so_far = 0
most_signature = nil
words.each_line do |word|
word = word.strip.downcase
signature = word.split('').sort.join('')
anagrams[signature] <<= word
anagrams[signature] = anagrams[signature] & anagrams[signature]
if anagrams[signature].length > most_so_far
most_signature = signature
most_so_far = anagrams[signature].length
end
end
return anagrams[most_signature]
end
puts find_longest_anagram.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment