LeetCode#205 Isomorphic Strings

Problem Definition:

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.

For example,
Given "egg", "add", return true.

Given "foo", "bar", return false.

Given "paper", "title", return true.

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

Solution: 利用字典 dict, 一次遍历,双向映射

 1 def isIsomorphic(s, t):
 2         d1,d2={},{}
 3         for a,b in zip(s,t):
 4             if a not in d1:
 5                 d1[a]=b
 6             else:
 7                 if d1[a]!=b:
 8                     return False
 9             if b not in d2:
10                 d2[b]=a
11             else:
12                 if d2[b]!=a:
13                     return False
14         return True
原文地址:https://www.cnblogs.com/acetseng/p/4654205.html