Word Pattern

Hash Table
Easy

Approach

Diagrams

Analysis

Time

O(n)

Space

O(m)
class Solution:
    def wordPattern(self, pattern, string):
        words = string.split(' ')

        bijectionNotPossible = not len(words) == len(pattern)

        if bijectionNotPossible:
            return False

        charsToWords = dict()

        for idx in range(len(words)):
            curChar = pattern[idx]
            matchingWord = words[idx]

            curCharMapsToOtherWord = curChar in charsToWords and charsToWords[curChar] != matchingWord
            matchingWordMapsToOtherChar = curChar not in charsToWords and matchingWord in charsToWords.values()

            if curCharMapsToOtherWord or matchingWordMapsToOtherChar:
                return False

            charsToWords[curChar] = matchingWord

        return True

Learn

#Hash Table

#Bijection

Videos