题目:
205.同构字符串
思路:
所谓同构,就是字符串的字符在位置和形式上应该一样。
用字典记录字符出现的次数;用str.index()比较字符出现的位置。
class Solution:def isIsomorphic(self, s: str, t: str) -> bool:# 同构:出现同一个字母的位置、数量要相同# 需要记录char的位置# 记录char的数量s_pointer, t_pointer = 0, 0s_dict, t_dict = {}, {}for s_char, t_char in zip(s, t):s_dict[s_char] = s_dict.get(s_char, 0) + 1t_dict[t_char] = t_dict.get(t_char, 0) + 1# 每一次比较对应位置的元素数量if s_dict[s_char] != t_dict[t_char] or s.index(s_char) != t.index(t_char):return Falsereturn True
