来源:
B站,北大陈斌老师《数据结构和算法》
算法四 计数比较
def anagramSolution4(s1,s2):
c1=[0]*26
c2=[0]*26
for i in range(len(s1)): #分别计数
pos=ord(s1[i])-ord('a')
c1[pos]=c1[pos]+1
for i in range(len(s2)):
pos=ord(s2[i])-ord('a')
c2[pos]=c2[pos]+1
j=0
stillOK=True
while j<26 and stillOK: #计数器比较
if c1[j]==c2[j]:
j=j+1
else:
stillOK=False
return stillOK
print(anagramSolution4('python','typhon'))