chenye-814 / DTSTRCT-ALGRTHM

0 stars 0 forks source link

Jan-01 Word Pattern #6

Open chenye-814 opened 1 year ago

chenye-814 commented 1 year ago

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

chenye-814 commented 1 year ago
view code ```py from collections import Counter class Solution: def wordPattern(self, pattern: str, s: str) -> bool: words = s.split(" ") if (len(pattern) != len(words) or len(Counter(pattern)) != len(Counter(words))): return False _dict = {} for i in range(len(pattern)): letter, word = pattern[i], words[i] if letter not in _dict: _dict[letter] = word elif _dict[letter] != word: return False return True ```