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: true


Example 2:


Input: s = "foo", t = "bar"
Output: false

Example 3:


Input: s = "paper", t = "title"
Output: true

Note:
You may assume both s and t have the same length.

Difficulty: introductory

Starter code:

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        

Solutions:
class Solution:
     def isIsomorphic(self, s1, s2):
         return len(set(zip(s1, s2))) == len(set(s1)) == len(set(s2))


class Solution:
     def isIsomorphic(self, s1, s2):
         """ Optimized version.
         Time complexity: O(n). Space complexity: O(1), n is len(s1) == len(s2).
         """
         # encode strings
         count1, count2 = 0, 0
         dict1, dict2 = dict(), dict()
         for i in range(len(s1)):
             char1, char2 = s1[i], s2[i]  # current characters
             if char1 in dict1 and char2 in dict2:
                 pass
             elif char1 not in dict1 and char2 not in dict2:
                 count1 += 1
                 count2 += 1
                 dict1[char1], dict2[char2] = count1, count2
             else:
                 return False
             curr1 = dict1[char1]  # current index of character in s1
             curr2 = dict2[char2]  # current index of character in s2
             if curr1 != curr2:
                 return False
         return True


class Solution:
     def isIsomorphic(self, s1, s2):
         """ Optimized version.
         Time complexity: O(n). Space complexity: O(1), n is len(s1) == len(s2).
         """
         # encode strings
         count1, count2 = 0, 0
         dict1, dict2 = dict(), dict()
         for i in range(len(s1)):
             if s1[i] in dict1:
                 curr1 = dict1[s1[i]]  # current index of character in s1
             else:
                 count1 += 1
                 dict1[s1[i]] = count1
                 curr1 = dict1[s1[i]]  # current index of character in s2
             if s2[i] in dict2:
                 curr2 = dict2[s2[i]]
             else:
                 count2 += 1
                 dict2[s2[i]] = count2
                 curr2 = dict2[s2[i]]
             if curr1 != curr2:
                 return False
         return True


class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         from collections import defaultdict as dict
         if len(s) != len(t):
             return False
         if not s and not t:
             return True
         ssd = dict(lambda: 100)
         sst = dict(lambda: 100)
         for ss, tt in zip(s, t):
             if ssd[ss] == 100:
                 ssd[ss] = ord(ss) - ord(tt)
             if sst[tt] == 100:
                 sst[tt] = ord(tt) - ord(ss)
             if ssd[ss] == ord(ss) - ord(tt) and sst[tt] == ord(tt) - ord(ss):
                 continue
             else:
                 return False
         return True


class Solution:
     def isIsomorphic(self, s1, s2):
         # encode strings
         enc1, enc2 = [], []
         count1, count2 = 0, 0
         dict1, dict2 = dict(), dict()
         for i in range(len(s1)):
             char1, char2 = s1[i], s2[i]
             if char1 in dict1:
                 enc1.append(dict1[char1])
             else:
                 count1 += 1
                 dict1[char1] = count1
                 enc1.append(dict1[char1])
             if char2 in dict2:
                 enc2.append(dict2[char2])
             else:
                 count2 += 1
                 dict2[char2] = count2
                 enc2.append(dict2[char2])
         return enc1 == enc2  # compare encodings


class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         map1 = {}
         map2 = {}
         for i in range(len(s)):
             if (s[i] in map1 and map1[s[i]] != t[i]) or (t[i] in map2 and map2[t[i]] != s[i]):
                 return False
             else:
                 map1[s[i]] = t[i]
                 map2[t[i]] = s[i]
         return True

class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         if len(s) == 0:
             return True
         current_s = s[0]
         current_t = t[0]
         switch_s = 0
         switch_t = 0
         s_dict = dict()
         t_dict = dict()
         for i in range(1, len(s)):
             if s[i] != current_s:
                 current_s = s[i]
                 switch_s += 1
             if t[i] != current_t:
                 current_t = t[i]
                 switch_t += 1
             if s[i] in s_dict:
                 if s_dict.get(s[i]) != t[i]:
                     return False
             if t[i] in t_dict:
                 if t_dict.get(t[i]) != s[i]:
                     return False
             s_dict.update({current_s: current_t})
             t_dict.update({current_t: current_s})
             if switch_s != switch_t:
                 return False
         return True

class Solution:
     def isIsomorphic(self, s1, s2):
         """ Optimized version.
         Time complexity: O(n). Space complexity: O(1), n is len(s1) == len(s2).
         """
         # encode strings
         count1, count2 = 0, 0
         dict1, dict2 = dict(), dict()
         for i in range(len(s1)):
             char1, char2 = s1[i], s2[i]  # current characters
             if char1 in dict1:
                 curr1 = dict1[char1]  # current index of character in s1
             else:
                 count1 += 1
                 dict1[char1] = count1
                 curr1 = dict1[char1]
             if char2 in dict2:
                 curr2 = dict2[char2]  # current index of character in s2
             else:
                 count2 += 1
                 dict2[char2] = count2
                 curr2 = dict2[char2]
             if curr1 != curr2:
                 return False
         return True


class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         l = len(s)
         if l != len(t):
             return False
         d = {}  #mapping
         for i, letter in enumerate(s):
             if letter not in d.keys():
                 d[letter] = t[i]
             else:
                 if d[letter] != t[i]:
                     return False
         
         d2 = {}  #mapping
         for i, letter in enumerate(t):
             if letter not in d2.keys():
                 d2[letter] = s[i]
             else:
                 if d2[letter] != s[i]:
                     return False
         return True

class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         mapping1 = dict()
     
         if len(s) != len(t):
             return False
         for i in range(len(s)):
             mapping1.setdefault(s[i],None)
             
             if mapping1[s[i]] == None:
                 mapping1[s[i]] = t[i]
             elif mapping1[s[i]] != t[i]:
                 return False
         if len(set(mapping1.values())) == len(list(mapping1.values())):
             return True
         return False
             
 
             


class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         from collections import defaultdict as dict
         if len(s) != len(t):
             return False
         if not s and not t:
             return True
         ssd = dict(lambda: 100)
         sst = dict(lambda: 100)
         for ss, tt in zip(s, t):
             if ssd[ss] == 100:
                 ssd[ss] = ord(ss) - ord(tt)
             if sst[tt] == 100:
                 sst[tt] = ord(tt) - ord(ss)
         for ss, tt in zip(s, t):
             if ssd[ss] == ord(ss) - ord(tt) and sst[tt] == ord(tt) - ord(ss):
                 continue
             else:
                 return False
         return True


class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         if len(s) != len(t): return False
         dic1, dic2 = {}, {}
         for i in range(len(s)):
             if s[i] in dic1 and dic1[s[i]] != t[i]:
                 return False
             if t[i] in dic2 and dic2[t[i]] != s[i]:
                 return False
             dic1[s[i]] = t[i]
             dic2[t[i]] = s[i]
         return True

class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         d1, d2 = [0 for _ in range(256)], [0 for _ in range(256)]
         for i in range(len(s)):
             if d1[ord(s[i])] != d2[ord(t[i])]:
                 return False
             d1[ord(s[i])] = i+1
             d2[ord(t[i])] = i+1
         return True

class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         """
    #    my second solution...modified based on first solution...
         l = len(s)
         
         sdic = {}
         snum = []
         i = 0
         j = 1
         while i < l:
             if s[i] not in sdic:
                 sdic[s[i]] = j
                 snum.append(j)
                 j += 1
             else:
                 snum.append(sdic[s[i]])
             i += 1
             
         tdic = {}
         tnum = []
         i = 0
         j = 1
         while i < l:
             if t[i] not in tdic:
                 if j != snum[i]:
                     return False
                 tdic[t[i]] = j
                 tnum.append(j)
                 j += 1
             else:
                 if tdic[t[i]] != snum[i]:
                     return False
                 tnum.append(tdic[t[i]])
             i += 1
         
         return True
         
         
         """
    #    my first solution...
         sdic = {}
         snum = []
         i = 0
         j = 1
         while i < len(s):
             if s[i] not in sdic:
                 sdic[s[i]] = j
                 snum.append(j)
                 j += 1
             else:
                 snum.append(sdic[s[i]])
             i += 1
             
         tdic = {}
         tnum = []
         i = 0
         j = 1
         while i < len(t):
             if t[i] not in tdic:
                 tdic[t[i]] = j
                 tnum.append(j)
                 j += 1
             else:
                 tnum.append(tdic[t[i]])
             i += 1
             
         if snum == tnum:
             return True
         else:
             return False


class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         if len(s) == 0:
             return True
         current_s = s[0]
         current_t = t[0]
         switch_s = 0
         switch_t = 0
         s_dict = dict()
         for i in range(1, len(s)):
             if s[i] != current_s:
                 current_s = s[i]
                 switch_s += 1
             if t[i] != current_t:
                 current_t = t[i]
                 switch_t += 1
             if s[i] in s_dict:
                 if s_dict.get(s[i]) != t[i]:
                     return False
             s_dict.update({current_s: current_t})
             if switch_s != switch_t:
                 return False
         return True

class Solution:
     def isIsomorphic(self, s, t):
         """
         :type s: str
         :type t: str
         :rtype: bool
         """
         d = dict()
         for (a,b) in zip(s,t):
             # new mapping
             if a not in d:
                 # duplicates in t
                 if b in d.values():
                     return False
                 else:
                     d[a] = b
             # old mapping
             else:
                 # no duplicate in t
                 if d[a] != b:
                     return False
         return True