Skip to content

Instantly share code, notes, and snippets.

@shelling
Created November 27, 2014 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelling/c79afbe23e2f3ce0abff to your computer and use it in GitHub Desktop.
Save shelling/c79afbe23e2f3ce0abff to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def hash(string)
hash = {}
string.each_char do |c|
hash[c] = hash[c].to_i + 1
end
return hash
end
def anagrams(source, target)
return false if source == target # identical words are not anagrams
source_hash, target_hash = hash(source), hash(target)
source_hash.keys.each do |key|
return false unless source_hash[key] == target_hash[key]
end
return source_hash.keys.length == target_hash.keys.length
end
puts anagrams("hello", "world")
puts anagrams("pluto", "toplu")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment