# 解题思路:字典解决其对应关系 20190302 找工作期间
#使用字典,pattern当key,str当value,形成配对
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: boo
"""
s = str.split()
if len(pattern) != len(s):
return False
return len(set(zip(pattern, s))) == len(set(pattern)) == len(set(s))
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
#使用字典,pattern当key,str当value,形成配对
dic = {}
strToList= str.split()
if len(pattern) != len(strToList) or len(set(pattern)) != len(set(strToList)):
return False
for i, val in enumerate(pattern):
if val not in dic:
dic[val] = strToList[i]
elif dic[val] != strToList[i]:
return False
return True