首页 > 其他 > 详细

leetcode-205-同构字符串

时间:2019-10-02 13:27:43      阅读:71      评论:0      收藏:0      [点我收藏+]

题目描述:

技术分享图片

 

 第一次提交:

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

优化:

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        hash = {}
        for i, c in enumerate(s):
            if hash.get(c):
                if t[i] != hash[c]:
                    return False
            else:
                if t[i] in hash.values():
                    return False
                hash[c] = t[i]
        return True

方法二:

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        return [*map(s.index, s)] == [*map(t.index, t)]

技术分享图片

 

leetcode-205-同构字符串

原文:https://www.cnblogs.com/oldby/p/11617055.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!