- 要求
前者是否包含后者的每个字符,
- 我的
1
2
3
4
5
6
7
8
9
10# 支持 集合运算,相减看是否包含 刷code war 题目 优化 看的
from collections import Counter
def scramble(s1, s2):
# Counter basically creates a dictionary of counts and letters
# Using set subtraction, we know that if anything is left over,
# something exists in s2 that doesn't exist in s1
return len(Counter(s2)- Counter(s1)) == 0
print(scramble('rkqodlw', 'world'),) - 别人的
1
2
3
4
5
6
7
8
9
10
11
12
13
14#1
def scramble(s1,s2):
for c in set(s2):
if s1.count(c) < s2.count(c):
return False
return True
# 2 多用库
from collections import Counter
from operator import sub
def scramble(s1,s2):
return not sub(*map(Counter, (s2,s1)))
# 2 特别的语法
scramble=lambda a,b,c=__import__('collections').Counter:not c(b)-c(a)