My Submission, Python code

  • 0
    class Solution(object):
        def wordPattern(self, pattern, s):
            :type pattern: str
            :type str: str
            :rtype: bool
            dic = {}
            dic_reverse = {}
            words = s.split(' ')
            if len(words) != len(pattern):
                return False
            for index in range(0, len(pattern)):
                if pattern[index] not in dic and words[index] not in dic_reverse:
                    dic[pattern[index]] = words[index]
                    dic_reverse[words[index]] = pattern[index]
                    if pattern[index] not in dic or dic[pattern[index]] != words[index]:
                        return False
                    if words[index] not in dic_reverse or dic_reverse[words[index]] != pattern[index]:
                        return False
            return True

Log in to reply

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