python - Understanding isomorphic strings algorithm -


i understanding following code find if strings isomorphic or not. code uses 2 hashes s_dict , t_dict respectively. assuming strings of same length.

def isisomorphic(s, t):     s_dict = {}     t_dict = {}     in range(len(s)):         if s[i] in s_dict.keys() , s_dict[s[i]] != t[i]:             return false         if t[i] in t_dict.keys() , t_dict[t[i]] != s[i]:             return false         s_dict[s[i]] = t[i]         t_dict[t[i]] = s[i]     return true 

now, if modify above code such 1 hash s_dict() used, gives desired results limited test cases. modified code follows:

def isisomorphic(s, t):     s_dict = {}     in range(len(s)):         if s[i] in s_dict.keys() , s_dict[s[i]] != t[i]:             return false         s_dict[s[i]] = t[i]     return true 

what test cases in above modified code fail? understanding of isomorphic strings wrong?

one simple example, code doesn't work on s='ab',t='aa'.

basically have have both way isomorphic. code checked t can modified s, not other way around.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -