题目
    205.同构字符串

    思路
    image.png
    所谓同构,就是字符串的字符在位置和形式上应该一样。
    用字典记录字符出现的次数;用str.index()比较字符出现的位置。

    1. class Solution:
    2. def isIsomorphic(self, s: str, t: str) -> bool:
    3. # 同构:出现同一个字母的位置、数量要相同
    4. # 需要记录char的位置
    5. # 记录char的数量
    6. s_pointer, t_pointer = 0, 0
    7. s_dict, t_dict = {}, {}
    8. for s_char, t_char in zip(s, t):
    9. s_dict[s_char] = s_dict.get(s_char, 0) + 1
    10. t_dict[t_char] = t_dict.get(t_char, 0) + 1
    11. # 每一次比较对应位置的元素数量
    12. if s_dict[s_char] != t_dict[t_char] or s.index(s_char) != t.index(t_char):
    13. return False
    14. return True