Skip to content

Instantly share code, notes, and snippets.

@imouaddine
Created November 6, 2014 15:12
Show Gist options
  • Save imouaddine/bfe2c315bf6cf6f96723 to your computer and use it in GitHub Desktop.
Save imouaddine/bfe2c315bf6cf6f96723 to your computer and use it in GitHub Desktop.
def isomorphic(a,b)
if a.length == 0 && b.length == 0
true
elsif a.length != b.length
false
else
a_stru = b_stru = Hash.new{|hash, key| hash[key] =[] }
b_stru = Hash.new{|hash, key| hash[key] =[] }
a_chars = a.split("")
b_chars = b.split("")
a_chars.each_with_index do |value, i|
a_stru[value] << i
b_stru[b_chars[i]] << i
end
a_stru = a_stru.values
b_stru = b_stru.values
a_stru.each_with_index.all?{|value, key| b_stru[key] == value }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment