leetcode——290.单词规律/205.同构字符串

简单题。同样的做法。

290.

class Solution:
    def wordPattern(self, pattern: str, str: str) -> bool:
        p={}
        q=str.split(' ')
        if len(pattern)!=len(q):
            return False
        for i in range(len(pattern)):
            if pattern[i] in p:
                if p[pattern[i]]!=q[i]:
                    return False
            else:
                if q[i] not in q[:i]:
                    p[pattern[i]]=q[i]
                else:
                    return False
        return True
执行用时 :32 ms, 在所有 python3 提交中击败了99.52%的用户
内存消耗 :13.9 MB, 在所有 python3 提交中击败了5.58%的用户
 
205.
class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        p={}
        for i in range(len(s)):
            if s[i] in p:
                if p[s[i]]!=t[i]:
                    return False
            else:
                if t[i] not in t[:i]:
                    p[s[i]]=t[i]
                else:
                    return False
        return True
执行用时 :44 ms, 在所有 python3 提交中击败了98.06%的用户
内存消耗 :14.1 MB, 在所有 python3 提交中击败了5.63%的用户
 
                                                                 ——2019.10.16

 

posted @ 2019-10-16 22:01  欣姐姐  阅读(152)  评论(0编辑  收藏  举报