class Solution:
def wordPattern(self, pattern: str, str: str) -> bool:
strList = str.split(' ')
if len(pattern) != len(strList): #避免数量不匹配的case
return False
dict_p = [-1] * 256
dict_s = {}
for i in range(len(pattern)):
if dict_p[ord(pattern[i])] == -1:
dict_p[ord(pattern[i])] = i
if strList[i] not in dict_s:
dict_s[strList[i]] = i
if dict_p[ord(pattern[i])] != dict_s[strList[i]]:
return False
return True