Created
May 21, 2024 21:32
-
-
Save ebanner/29e59e0eef53319636297dd7cbab550e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# @param {String} s | |
# @param {String} t | |
# @return {Boolean} | |
def is_isomorphic(s, t) | |
numeric_s = convert_to_numeric(s) | |
numeric_t = convert_to_numeric(t) | |
numeric_s == numeric_t | |
# More explicit version | |
# isomorphic = numeric_s == numeric_t | |
# isomorphic | |
end | |
# def isomorphic?(numeric_s, numeric_t) | |
# numeric_s == numeric_t | |
# end | |
def convert_to_numeric(str) | |
map = {} | |
counter = 1 | |
list = [] | |
str.each_char do |c| | |
unless map.has_key?(c) | |
map[c] = counter | |
counter += 1 | |
end | |
list << map[c] | |
end | |
list | |
end | |
# puts is_isomorphic("egg", "add") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment