class Solution:
def wordPattern(self, pattern, str):
slist = str.split()
return map(pattern.index, pattern) == map(slist.index, slist)
同位置映射,使用zip
class Solution:
def wordPattern(self, pattern, str):
slist = str.split()
if len(pattern) != len(slist):
return False
return len(set(pattern)) == len(set(slist)) == len(set(zip(pattern, slist)))