def different(t,s):
t1 = list(t)
s1 = list(s)
t1.sort()
s1.sort()
return t1 == s1
def different1(t,s):
dict1 = {}
dict2 = {}
for i in t:
dict1[i] = dict1.get(i,0)+1
for i in s:
dict2[i] = dict2.get(i,0)+1
return dict1 == dict2
print(different('anagram','nagaraf'))
print(different1('anagram','nagaraf'))
两字符串是否相同--牛客
最新推荐文章于 2024-11-04 21:51:40 发布
本文探讨了两种Python函数`different`和`different1`,分别用于比较字符串是否为异构词(anagram)和字符频率是否相等。通过列表排序和字典计数,展示了如何利用基本数据结构实现字符串比较。
摘要由CSDN通过智能技术生成