'''
keys: 
Solutions:
Similar: 
T:
S:
'''
from typing import List


class Solution:
    
    # concise, brilliant
    def wordPattern1(self, pattern: str, str: str) -> bool:
        s, p = str.split(), list(pattern)
        # str.index returns the first occurrence of the "char" in the word;
        return list(map(s.index, s)) == list(map(p.index, p))
    

    
    # self-made, one pass
    def wordPattern(self, pattern: str, str: str) -> bool:
        
        if not pattern or not str: return False
        
        hashmap = {}
        strs = str.split(" ")
        if len(pattern) != len(str.split(" ")): 
            return False
        for idx, p in enumerate(pattern):
            if p not in hashmap:
                if strs[idx] not in hashmap.values():
                    hashmap[p] = strs[idx]
                else:
                    return False
            else:
                if hashmap[p] != strs[idx]:
                    return False
        return True
    
    
    # two hashmap
    def wordPattern2(self, pattern: str, str: str) -> bool:
        map_char = {}
        map_word = {}
        
        words = str.split(' ')
        if len(words) != len(pattern):
            return False
        
        for c, w in zip(pattern, words):
            if c not in map_char:
                if w in map_word:
                    return False
                else:
                    map_char[c] = w
                    map_word[w] = c
            else:
                if map_char[c] != w:
                    return False
        return True
    
    