Python Solution for 290. Word Pattern

  • 0

    class Solution:
    def wordPattern(self, pattern, string):
    :type pattern: str
    :type str: str
    :rtype: bool
    wordList = string.split()

        if len(wordList) != len(pattern):
            return False
        dict_info = {}
        for i in range(len(pattern)):
            if pattern[i] not in dict_info:
                if wordList[i] not in dict_info.values():
                    dict_info[pattern[i]] = wordList[i]
                    return False
                if (dict_info[pattern[i]] != wordList[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.