205. 同构字符串

205. 同构字符串

运用哈希表解决此题

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        dict = {}
        for i in range(len(s)):
            if s[i] not in dict:
                if t[i] in dict.values():
                    return False
                else:
                    dict[s[i]] = t[i]
            else:
                if dict[s[i]] != t[i]:
                    return False
        return True


if __name__ == "__main__":
    a = Solution()
    s = input("请输入字符串s:")
    t = input("请输入字符串t:")
    print(a.isIsomorphic(s,t))

你可能感兴趣的:(算法刷题,字符串,python)