变位词-全检索-字典

def anagramSolution5(s1,s2):
    list1 = [x for x in range(24)]
c1 = {key: 0 for key in list1}
c2 = {key: 0 for key in list1}

for i in range(len(s1)):
pos = ord(s1[i]) - ord('a')
if pos in c1: # 存在
c1[pos] += 1 # 叠加
else:
c1[pos] = 1 # 不存在,再新建
c2[pos] = 0 #新建

for i in range(len(s2)):
pos = ord(s2[i]) - ord('a')
if pos in c2: # 存在
c2[pos] += 1 # 叠加
else:
if pos in c1:
c1[pos] += 1
else:
return False
c2[pos] = 1 # 不存在,再新建


for key in c1:
if c1[key] == c2[key]:
continue
else:
return False

return True


print("字典",anagramSolution5('中文字符','字符中文'))
print("字典",anagramSolution5('中文强大2e','中文强大字符'))
print("字典",anagramSolution5('中文强大','中文强大##'))
print("字典",anagramSolution5('中文强大##','中文强大##'))
posted @ 2020-03-13 21:30  simon羊  阅读(187)  评论(0)    收藏  举报