本帖最后由 洋洋痒 于 2021-3-21 23:12 编辑 def wordPattern(pattern, s):
if len(set(pattern))!=len(set(s.split())) or len(pattern)!=len(s.split()):
return False
L=s.split()
count=0
s=len(L)-len(set(L))
for i in range(len(L)):
count+=1
if count>s:
return True
for j in range(i+1,len(L)):
if L[j]==L[i] and pattern[j]!=pattern[i]:
return False
return True
print(wordPattern("abba","dog cat cat dog"))
print(wordPattern("abba","dog cat cat fish"))
print(wordPattern("aaaa","dog cat cat dog"))
print(wordPattern("aaaa","dog dog dog dog"))
我猜又会超时 |