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.

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Data structures

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):
		(count1, count2) = (0, 0)
		(dict1, dict2) = (dict(), dict())
		for i in range(len(s1)):
			(char1, char2) = (s1[i], s2[i])
			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]
			curr2 = dict2[char2]
			if curr1 != curr2:
				return False
		return True


class Solution:

	def isIsomorphic(self, s1, s2):
		(count1, count2) = (0, 0)
		(dict1, dict2) = (dict(), dict())
		for i in range(len(s1)):
			if s1[i] in dict1:
				curr1 = dict1[s1[i]]
			else:
				count1 += 1
				dict1[s1[i]] = count1
				curr1 = dict1[s1[i]]
			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):
		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):
		(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


class Solution:

	def isIsomorphic(self, s, t):
		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):
		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, s, t):
		l = len(s)
		if l != len(t):
			return False
		d = {}
		for (i, letter) in enumerate(s):
			if letter not in d.keys():
				d[letter] = t[i]
			elif d[letter] != t[i]:
				return False
		d2 = {}
		for (i, letter) in enumerate(t):
			if letter not in d2.keys():
				d2[letter] = s[i]
			elif d2[letter] != s[i]:
				return False
		return True


class Solution:

	def isIsomorphic(self, s, t):
		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):
		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):
		(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):
		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):
		d = dict()
		for (a, b) in zip(s, t):
			if a not in d:
				if b in d.values():
					return False
				else:
					d[a] = b
			elif d[a] != b:
				return False
		return True