Created
November 27, 2014 14:13
-
-
Save shelling/c79afbe23e2f3ce0abff to your computer and use it in GitHub Desktop.
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
#!/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