Share Python with two Hash


  • 0
    W
    class Solution(object):
        def wordPattern(self, pattern, str):
            """
            :type pattern: str
            :type str: str
            :rtype: bool
            """
            strx=str.split()
            p=list(pattern)
            dict={}
            dict2={}
            if len(strx) !=len(p):
                return False
            for i in range(len(p)):
                if p[i] in dict and dict[p[i]] !=strx[i]:
                    return False
                if strx[i] in dict2 and dict2[strx[i]] !=p[i]:
                    return False
                if p[i] not in dict:
                    dict[p[i]]=strx[i]
                if strx[i] not in dict2:
                    dict2[strx[i]]=p[i]
            return True

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.