205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
Example 1:
Input: s = "egg", t = "add"
Output: trueExample 2:
Input: s = "foo", t = "bar"
Output: falseExample 3:
Input: s = "paper", t = "title"
Output: trueNote: You may assume both s and t have the same length.
# @param {String} s
# @param {String} t
# @return {Boolean}
def is_isomorphic(s, t)
return false if s.size != t.size
hash1 = {}
hash2 = {}
for i in 0..s.size do
curn1 = s[i]
curn2 = t[i]
if hash1[curn1].nil?
hash1[curn1] = curn2
elsif hash1[curn1] != curn2
return false
end
if hash2[curn2].nil?
hash2[curn2] = curn1
elsif hash2[curn2] != curn1
return false
end
end
return true
endLast updated
Was this helpful?