Skip to main content

290. Word Pattern

https://leetcode.com/problems/word-pattern/

Python

class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
mapper = dict()
words = s.split(' ')

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

for p, word in zip(pattern, words):
if word not in mapper:
mapper[word] = p
continue

if mapper[word] != p:
return False

return len(mapper.values()) == len(set(mapper.values()))