看看答案
def compare(s1,s2):
for i in s1:
if i not in s2:
return False
for j in s2:
if j not in s1:
return False
return True
s1 = 'good day'
s2 = 'goodday'
s3 = 'gdooa dy'
print(compare(s1,s2))
print(compare(s1,s3))
结果:
False
True
{:5_95:}
def test_no_repeat(s1,s2):
l1 = list(s1)
l2 = list(s2)
l1.sort()
l2.sort()
return l1 == l2
def test_repeat(s1,s2):
return set(s1) == set(s2)
def fun1(s1, s2): # s1内字符可以重复使用
s = set(s2)
for i in s:
if i not in s1:
return False
return True
def fun2(s1, s2):# s1内字符不可以重复使用
# 计算s2内字符种类及个数
s = set(s2)
dictS2 = {}
for i in s:
dictS2 = s2.count(i)
# S1中相应的字符个数应该大于等于S2中字符个数
enough = []
for i in dictS2:
if s1.count(i) >= dictS2:
enough.append(True)
else:
enough.append(False)
if sum(enough) == len(s):
return True
else:
return False
print(fun1('abct','bat')) # True
print(fun2('abt','batt')) # False
看看
本帖最后由 永恒的蓝色梦想 于 2019-8-20 11:22 编辑
可以重复使用:lambda s1,s2:all((i in s1 for i in set(s2)))
不可以重复使用:lambda s1,s2:all((s1.count(i)>=s2.count(i)for i in set(s2)))
学习来了。
{:10_256:}
新手,借鉴一下,嘿嘿
{:10_277:}
from collections import Counter
# 可重复使用
def f_79_dup_use(str1, str2):
return True if Counter(set(str1)) == Counter(set(str2)) else False
# 不可重复使用
def f_79_no_dup_use(str1, str2):
return True if Counter(str1) == Counter(str2) else False
print(f_79_dup_use('ohel', 'hello'))
print(f_79_no_dup_use('ohel', 'hello'))
使用permutations模块
1
学习