"""
290
easy
word pattern


"""

class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:

        p = list(pattern)
        words = s.split(" ")
        hash1 = {}
        hash2 = {}

        if len(p) != len(words):
            return False

        for i in range(len(p)):
            if p[i] not in hash1 and words[i] not in hash2:
                hash1[p[i]] = words[i]
                hash2[words[i]] = p[i]
            elif p[i] not in hash1 or words[i] not in hash2:
                return False
            else:
                if words[i] != hash1[p[i]]:
                    return False
        return True

pattern = "abba"
s = "dog cat cat dog"
sol = Solution()
print(sol.wordPattern(pattern, s))

"""
290
easy
word pattern


"""

class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:

        p = list(pattern)
        words = s.split(" ")
        hash1 = {}
        hash2 = {}

        if len(p) != len(words):
            return False

        for i in range(len(p)):
            if p[i] not in hash1 and words[i] not in hash2:
                hash1[p[i]] = words[i]
                hash2[words[i]] = p[i]
            elif p[i] not in hash1 or words[i] not in hash2:
                return False
            else:
                if words[i] != hash1[p[i]]:
                    return False
        return True

pattern = "abba"
s = "dog cat cat dog"
sol = Solution()
print(sol.wordPattern(pattern, s))

290 easy word pattern