My accepted python solution using dictionary function


  • 0
    Y
     def wordPattern(self, pattern, str):
            """
            :type pattern: str
            :type str: str
            :rtype: bool
            """
            str_list=str.split(' ')
            if len(pattern)!=len(str_list):return False
            dic={}
            result=True
            for i in range(len(pattern)):
                if (pattern[i] not in dic):
                    if (str_list[i] not in dic.values()):
                        dic[pattern[i]]=str_list[i]
                    else: return False
                elif (dic[pattern[i]]!=str_list[i]):
                    return False
           return True
    

Log in to reply
 

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