human
Solve in Python:
Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Example 1:

Input: pattern = "abba", str = "dog cat cat dog"
Output: true

Example 2:

Input:pattern = "abba", str = "dog cat cat fish"
Output: false

Example 3:

Input: pattern = "aaaa", str = "dog cat cat dog"
Output: false

Example 4:

Input: pattern = "abba", str = "dog dog dog dog"
Output: false

Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
 What is a Python program to find if str follows the same pattern?
assistant
#Here is Python program to find if str follows the same pattern.

class Solution:
     def wordPattern(self, pattern, str):
         if not pattern and not str:
             return True
         if not pattern or not str:
             return False
         
         str_list = str.split()
         if len(pattern) != len(str_list):
             return False
         
         pattern_table = collections.defaultdict(int)
         str_table = collections.defaultdict(int)
         for idx in range(len(pattern)):
             if pattern_table[pattern[idx]] != str_table[str_list[idx]]:
                 return False
             pattern_table[pattern[idx]] = idx + 1
             str_table[str_list[idx]] = idx + 1
             
         return True
         
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """