Likou NO.383 Ransom Letter (Hash Table: Use of Counter()) NO.242 Valid Alphabets

topic ( link )

insert image description here

Method 1: Hash table: the use of Counter()

import collections
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        # magezine长度不够,直接return False
        if len(ransomNote)>len(magazine):
            return False
        # 如果完全匹配,返回True
        return not collections.Counter(ransomNote)-collections.Counter(magazine)

Topics ( 242 topic links )

insert image description here

Method 1: Same as above, the use of Counter()

import collections
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        # 知识点:此题没有用到,因为 Counter 实现了字典的 __missing__ 方法, 所以当访问不存在的key的时候,返回值为0:
        # 长度不同直接 return False
        if(len(s)!=len(t)):return False
        return not collections.Counter(s)-collections.Counter(t)

Guess you like

Origin blog.csdn.net/qq_33489955/article/details/124272838