网站优化外包服务,欧美网站建设排名大全,博客网站源码,有什么管理系统【力扣题】题目描述#xff1a; 题解#xff1a;
两个字符串ransomNote和magazine#xff0c;ransomNote中每个字母都在magazine中一一对应#xff08;顺序可以不同#xff09;。
即分别统计两个字符串中每个字母出现的次数#xff0c;ransomNote中每个字母的个数小于等…【力扣题】题目描述 题解
两个字符串ransomNote和magazineransomNote中每个字母都在magazine中一一对应顺序可以不同。
即分别统计两个字符串中每个字母出现的次数ransomNote中每个字母的个数小于等于magazine中该字母对应的个数。 【Python3】代码
1、解题思路使用collections.Counter()分别统计两字符串的字母及出现次数比较相同字母的个数。
1-1若某字母在ransomNote中的个数大于在magazine中的个数则有元素在magazine字符串中不存在。
知识点len(...)获取序列字符串、列表等的长度。 collections.Counter(...)字典子类计数器统计元素和元素出现的次数。 字典.items()返回可迭代的字典的所有键值对键值对是元组形式 (键, 值)。 字典[键]获取字典中键对应的值。也可给键赋值或修改值字典[键]值。
class Solution:def canConstruct(self, ransomNote: str, magazine: str) - bool:from collections import Counterif len(ransomNote) len(magazine): return Falsea, b Counter(ransomNote), Counter(magazine)for k,v in a.items():if a[k] b[k]:return Falsereturn True 1-2两个计数器相减即相同的键对应的值相减若相减后仍有元素的值大于0则有元素在magazine字符串中不存在。
知识点collections.Counter(可迭代对象1) - collections.Counter(可迭代对象2)两个可迭代对象中相同元素的值相减只返回结果值大于0的元素。
class Solution:def canConstruct(self, ransomNote: str, magazine: str) - bool:from collections import Counterif len(ransomNote) len(magazine): return Falsereturn not Counter(ransomNote) - Counter(magazine)
注解 2、解题思路用一个字典记录每个字母的个数。
2-1字符串ransomNote中统计每个字母的个数字符串magazine将字母对应的个数从字典中减去。若最终字典中有字母的值大于0则有元素在magazine字符串中不存在。
知识点collections.defaultdict(...)字典子类若字典中某键不存在则调用工厂函数返回默认值。 字典.values()返回可迭代的字典的所有值。
class Solution:def canConstruct(self, ransomNote: str, magazine: str) - bool:from collections import defaultdictif len(ransomNote) len(magazine): return Falsed defaultdict(int)for x in ransomNote:d[x] 1for y in magazine:d[y] - 1for val in d.values():if val 0:return Falsereturn True
2-2两个字符串反过来统计字符串magazine中统计每个字母的个数字符串ransomNote将字母对应的个数从字典中减去若减去后该字母的值小于0则有元素在magazine字符串中不存在。
class Solution:def canConstruct(self, ransomNote: str, magazine: str) - bool:from collections import defaultdictif len(ransomNote) len(magazine): return Falsed defaultdict(int)for y in magazine:d[y] 1for x in ransomNote:d[x] - 1if d[x] 0:return Falsereturn True 3、解题思路用一个列表记录每个字母的个数。以字母距离“a”的间隔作为下标。字符串ransomNote中统计每个字母总个数字符串magazine将字母对应的个数从列表中减去。若结果中有元素大于0则有元素在magazine字符串中不存在。
3-1知识点[0]*26即长度为26的元素都是0的列表[0,0,...0,0]。 ord(...)获取字符的ascii值或unicode值。 列表[下标]获取列表中下标对应的元素。也可修改下标对应的元素值列表[下标]值。
class Solution:def canConstruct(self, ransomNote: str, magazine: str) - bool:if len(ransomNote) len(magazine): return False alist [0] * 26 for x in ransomNote:alist[ord(x) - ord(a)] 1for y in magazine:alist[ord(y) - ord(a)] - 1for val in alist:if val 0:return Falsereturn True
3-2知识点itertools.chain(可迭代对象1, 可迭代对象2,...)返回一个迭代器包含多个可迭代对象的所有元素。 enumerate(...)返回可迭代的所有元素下标和元素元组形式 (下标, 元素)。
class Solution:def canConstruct(self, ransomNote: str, magazine: str) - bool:from itertools import chainn len(ransomNote)if n len(magazine): return Falsealist [0] * 26 for i,x in enumerate(chain(ransomNote,magazine)):if i n: alist[ord(x)-ord(a)] 1else:alist[ord(x)-ord(a)] - 1for val in alist:if val 0:return Falsereturn True
3-3知识点itertools.zip_longest(可迭代对象1, 可迭代对象2, fillvalueNone)返回一个迭代器将两个可迭代对象的元素按对应位置一一组成元组。所有元素遍历完若其中一个可迭代对象没有元素了可用fillvalue参数指定默认值来填充空缺若没有指定fillvalue则用None填充空缺。
class Solution:def canConstruct(self, ransomNote: str, magazine: str) - bool:from itertools import zip_longestif len(ransomNote) len(magazine): return False alist [0] * 26for x,y in zip_longest(ransomNote,magazine,fillvalue0):if x ! 0: alist[ord(x)-ord(a)] 1if y ! 0: alist[ord(y)-ord(a)] - 1for val in alist:if val 0:return Falsereturn True 4、解题思路将字符串ransomNote中字母去重再遍历元素比对字母在两个字符串中出现的次数若所有字母在ransomNote中的个数都小于magazine中的个数则返回True。
知识点set(...)转为集合集合中的元素不重复。 序列.count(...)统计元素在序列字符串列表等中出现的次数。 all(...)判断可迭代对象元组列表中的所有元素是否都为True。
class Solution:def canConstruct(self, ransomNote: str, magazine: str) - bool:if len(ransomNote) len(magazine): return False return all(ransomNote.count(x) magazine.count(x) for x in set(ransomNote))