Question: https://leetcode.com/problems/isomorphic-strings/
Question Name: Isomorphic Strings
The key point is to avoid 2+ characters map to the same character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Solution(object): def isIsomorphic(self, s, t): """ :type s: str :type t: str :rtype: bool """ mapping = {} for index in xrange(len(s)): if not s[index] in mapping: # New mapping is found. mapping[s[index]] = t[index] elif mapping[s[index]] != t[index]: # No 2+ characters may map to the same character. return False # Make sure that no 2+ characters may map to the same character. if len(set(mapping.keys())) != len(set(mapping.values())): return False return True |