LeetCode 290 Word Pattern

LeetCode 290 Word Pattern Problem

Download Code
class Solution(object):
    def wordPattern(self, pattern, str):
        """
        :type pattern: str
        :type str: str
        :rtype: bool
        """
        if pattern is None or str is None:
            return True
        # double map
        words_to_pattern = {}
        pattern_to_words = {}
        word_list = str.split(' ')
        if len(word_list) != len(pattern):
            return False
        for index, word in enumerate(word_list):
            curr_p = pattern[index]
            if pattern_to_words.get(curr_p, word) != word or words_to_pattern.get(word, curr_p) != curr_p:
                return False
            pattern_to_words[curr_p] = pattern_to_words.get(curr_p, word)
            words_to_pattern[word] = words_to_pattern.get(word, curr_p)
        return True
Download Word Pattern.py

List of all Word Pattern problems

Leetcode 290 Word Pattern problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.