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.

Difficulty: introductory

Starter code:

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

Solutions:
class Solution:
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         # d['a'] = word
         #pattern_arr = list(pattern)
         str_arr = str.split()
         pattern_dict = {}
         str_dict = {}
         
         pattern_tokenize = []
         token_p = 0
         
         str_tokenize = []
         token_s = 0
         
         for char in pattern:
             if char not in list(pattern_dict.keys()):
                 pattern_dict[char] = token_p
                 token_p += 1
                 pattern_tokenize.append(token_p)
             else:
                 pattern_tokenize.append(pattern_dict[char])
                 
         for word in str_arr:
             if word not in list(str_dict.keys()):
                 str_dict[word] = token_s
                 token_s += 1
                 str_tokenize.append(token_s)
             else:
                 str_tokenize.append(str_dict[word])
             
         return (pattern_tokenize == str_tokenize)
         
         


class Solution:
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         dict1 = {i:[] for i in pattern}
         for tag,i in enumerate(pattern):
             dict1[i].append(tag)
         L1 = []
         for key in dict1:
             L1.append(dict1[key])
             
         dict2 = {i:[] for i in str.split()}
         for tag,i in enumerate(str.split()):
             dict2[i].append(tag)
         L2 = []
         for key in dict2:
             L2.append(dict2[key])
 
         if sorted(L1) == sorted(L2):
             return True
         else:
             return False
         
             


class Solution:
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         return list(map(pattern.find, pattern)) == list(map(str.split().index, str.split()))

class Solution(object):
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         ptMap = {}
         good = True
         wList = str.split(' ')
         if len(wList) != len(pattern):
             good = False
         else:
             for k, v in enumerate(wList):
                 if v in ptMap:
                     if ptMap[v] != pattern[k]:
                         good = False
                 else:
                     if pattern[k] in ptMap.values():
                         good = False
                     else:
                         ptMap[v] = pattern[k]
 
         return good

class Solution:
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         
         
         strlst = str.split()
         return [ pattern.find(i) for i in pattern ] == [ strlst.index(i) for i in strlst ]
     
     
         
         """
         my first solution...beat 93%
         
         strlst = str.split()
         
         if len(pattern) != len(strlst) or len(pattern) == 0:
             return False
         if len(pattern) == 1:
             return True
         
         pdic = {}
         pnum = []
         sdic = {}
         snum = []
         i = 0
         j = 0
         k = 0
         while i < len(pattern):
             if pattern[i] not in pdic:
                 pdic[pattern[i]] = j
                 pnum.append(j)
                 j += 1
             else:
                 pnum.append(pdic[pattern[i]])
             if strlst[i] not in sdic:
                 sdic[strlst[i]] = k
                 snum.append(k)
                 k += 1
             else:
                 snum.append(sdic[strlst[i]])
             i += 1
         
         if pnum == snum:
             return True
         return False
         """

class Solution:
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         str_ls = str.split()
         flag = True
         pattern_d, str_d = {}, {}
         if len(str_ls) != len(pattern):
             return False
         for i in range(len(pattern)):
             if (str_ls[i] in str_d) and (pattern[i] in pattern_d):
                 if (str_d[str_ls[i]] != pattern[i]) or (pattern_d[pattern[i]] != str_ls[i]):
                     flag = False
             elif (pattern[i] not in pattern_d) and (str_ls[i] not in str_d):
                 pattern_d[pattern[i]] = str_ls[i]
                 str_d[str_ls[i]] = pattern[i]
 
             else:
                 flag = False
         return flag


class Solution:
     def wordPattern(self, pattern, s):
         """
         :type pattern: str
         :type s: str
         :rtype: bool
         """
         words = s.split(' ')
         if len(words) != len(pattern):
             return False
         
         d = {}
         for a, b in zip(pattern, words):
             if a not in d:
                 d[a] = b
             else:
                 if d[a] != b:
                     return False
                 
         values = list(d.values())
         return len(values) == len(set(values))


class Solution:
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         pattern_map = {}
         pattern_word = []
         pattern_result = []
         string_map = {}
         string_word = []
         string_result = []
         for index,item in enumerate(pattern):
             if item not in pattern_map:
                 pattern_word.append(item)
             pattern_map[item] = pattern_map.get(item,[]) + [index]
         for item in pattern_word:
             pattern_result.append([pattern_map[item]])
         start = 0
         count = 0
         index = 0
         while index <= len(str):
             if index < len(str) and str[index] != ' ':
                 index += 1
                 continue
             else:
                 sub_str = str[start:index]
                 index += 1
                 if sub_str not in string_map:
                     string_word.append(sub_str)
                 string_map[sub_str] = string_map.get(sub_str,[]) + [count]
                 start = index
                 count += 1
         for item in string_word:
             string_result.append([string_map[item]])
         if string_result == pattern_result:
             return True
         else:
             return False
                 
             


class Solution:
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         if pattern is None or str is None:
             return False
         
         dic_p2w = {}
         pset = set()
         words = str.split(' ')
         if len(pattern) != len(words):
             return False
         
         for i in range(len(words)):
             pset.add(pattern[i])
             if pattern[i] not in dic_p2w:
                 dic_p2w[pattern[i]] = words[i]
             else:
                 if dic_p2w[pattern[i]] != words[i]:
                     return False
         return len(pset) == len(set(words)) 

class Solution:
     def wordPattern(self, pattern, str):
         """
         :type pattern: str
         :type str: str
         :rtype: bool
         """
         wordlist = str.split()
         dictionary = {}
         L1 = len(pattern)
         L2 = len(wordlist)
         if L1 !=L2 :
             return False
         else:
             for n in range(L1):
                 #dictionary key exists already, just check for value
                 if pattern[n] in dictionary.keys():
                     if(dictionary[pattern[n]] != wordlist[n]):
                         return False
                 else:
                     #dictionary key does not exist
                     if (wordlist[n] not in dictionary.values()):
                         dictionary[pattern[n]] =wordlist[n]
                     else:
                         return False
         return True

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
         """