""" 0205.1 - Solution 1 - Hash Map (Two Dictionaries) """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isIsomorphic(self, s: str, t: str) -> bool:
"""Isomorphic Strings Function"""
map_s_to_t = {}
map_t_to_s = {}
for char_s, char_t in zip(s, t):
if char_s in map_s_to_t:
if map_s_to_t[char_s] != char_t:
return False
else:
map_s_to_t[char_s] = char_t
if char_t in map_t_to_s:
if map_t_to_s[char_t] != char_s:
return False
else:
map_t_to_s[char_t] = char_s
return True
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isIsomorphic("egg", "add")) # True
print(Solution().isIsomorphic("foo", "bar")) # False
print(Solution().isIsomorphic("paper", "title")) # True
print(Solution().isIsomorphic("badc", "baba")) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0205.2 - Solution 2 - Index Mapping """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isIsomorphic(self, s: str, t: str) -> bool:
"""Isomorphic Strings Function"""
index_s = {}
index_t = {}
for i, (char_s, char_t) in enumerate(zip(s, t)):
if index_s.get(char_s) != index_t.get(char_t):
return False
index_s[char_s] = i + 1
index_t[char_t] = i + 1
return True
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isIsomorphic("egg", "add")) # True
print(Solution().isIsomorphic("foo", "bar")) # False
print(Solution().isIsomorphic("paper", "title")) # True
print(Solution().isIsomorphic("badc", "baba")) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()