word_pattern
1# @leet start 2class Solution: 3 def wordPattern(self, pattern: str, s: str) -> bool: 4 """ 5 This problems involves defining a bijection (1:1) mapping between the words in `s` and the pattern. 6 To do this, we split up the `s` into words, and then start iterating through both of them. 7 If they both always equal each other, then we return true, otherwise, return false. 8 """ 9 pattern_to_word = {} 10 word_to_pattern = {} 11 words = s.split(' ') 12 # bail early 13 if len(pattern) != len(words): 14 return False 15 16 for i, word in enumerate(words): 17 curr_pattern = pattern[i] 18 if curr_pattern not in pattern_to_word and word not in word_to_pattern: 19 pattern_to_word[curr_pattern] = word 20 word_to_pattern[word] = curr_pattern 21 continue 22 if curr_pattern not in pattern_to_word or word not in word_to_pattern: 23 return False 24 if pattern_to_word[curr_pattern] != word or word_to_pattern[word] != curr_pattern: 25 return False 26 27 return True 28# @leet end 29sol = Solution() 30 31def test(): 32 assert(sol.wordPattern("abba", "dog cat cat dog") == True) 33 assert(sol.wordPattern("abba", "dog cat cat fish") == False) 34 assert(sol.wordPattern("aaaa", "dog cat cat dog") == False) 35 assert(sol.wordPattern("abba", "dog dog dog dog") == False)
class
Solution:
3class Solution: 4 def wordPattern(self, pattern: str, s: str) -> bool: 5 """ 6 This problems involves defining a bijection (1:1) mapping between the words in `s` and the pattern. 7 To do this, we split up the `s` into words, and then start iterating through both of them. 8 If they both always equal each other, then we return true, otherwise, return false. 9 """ 10 pattern_to_word = {} 11 word_to_pattern = {} 12 words = s.split(' ') 13 # bail early 14 if len(pattern) != len(words): 15 return False 16 17 for i, word in enumerate(words): 18 curr_pattern = pattern[i] 19 if curr_pattern not in pattern_to_word and word not in word_to_pattern: 20 pattern_to_word[curr_pattern] = word 21 word_to_pattern[word] = curr_pattern 22 continue 23 if curr_pattern not in pattern_to_word or word not in word_to_pattern: 24 return False 25 if pattern_to_word[curr_pattern] != word or word_to_pattern[word] != curr_pattern: 26 return False 27 28 return True
def
wordPattern(self, pattern: str, s: str) -> bool:
4 def wordPattern(self, pattern: str, s: str) -> bool: 5 """ 6 This problems involves defining a bijection (1:1) mapping between the words in `s` and the pattern. 7 To do this, we split up the `s` into words, and then start iterating through both of them. 8 If they both always equal each other, then we return true, otherwise, return false. 9 """ 10 pattern_to_word = {} 11 word_to_pattern = {} 12 words = s.split(' ') 13 # bail early 14 if len(pattern) != len(words): 15 return False 16 17 for i, word in enumerate(words): 18 curr_pattern = pattern[i] 19 if curr_pattern not in pattern_to_word and word not in word_to_pattern: 20 pattern_to_word[curr_pattern] = word 21 word_to_pattern[word] = curr_pattern 22 continue 23 if curr_pattern not in pattern_to_word or word not in word_to_pattern: 24 return False 25 if pattern_to_word[curr_pattern] != word or word_to_pattern[word] != curr_pattern: 26 return False 27 28 return True
This problems involves defining a bijection (1:1) mapping between the words in s and the pattern.
To do this, we split up the s into words, and then start iterating through both of them.
If they both always equal each other, then we return true, otherwise, return false.
sol =
<Solution object>
def
test():