a simply solution in python


  • 0
    F

    the basic idea about my solution is :the pattern's and the word's which in corresponed place have the same index queue.the code is under:

    class Solution(object):
        def wordPattern(self, pattern, str):
            pattern_dic = {}
            str_dic={}
            for i,v in enumerate(pattern):
                pattern_dic[v]=pattern_dic.get(v,[])+[i]
            for i,v in enumerate(str.split()):
                str_dic[v]=str_dic.get(v,[])+[i]
            return sorted(pattern_dic.values())==sorted(str_dic.values())
    

Log in to reply
 

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