什么是确定一个字符串/集合是否是另一个子集的最佳方法?

huangapple go评论68阅读模式
英文:

What's the best way to determine if one string / collection is a subset of another?

问题

给定以下问题,实现解决方案的最短方法是什么?

给定两个字符串 ransomNote 和 magazine,如果可以使用 magazine 中的字母构建 ransomNote,则返回 true,否则返回 false。ransomNote 中的每个字母只能在 magazine 中使用一次。

肯定有比手动计算每个字符更好的方法吧?

def canConstruct(self, ransomNote: str, magazine: str) -> bool:
    c1, c2 = Counter(ransomNote), Counter(magazine)
    for letter in c1:
        if not (letter in c2 and c2[letter] >= c1[letter]):
            return False
        
    return True

如果您需要进一步的翻译或其他帮助,请告诉我。

英文:

For example, given the following problem, what's the shortest way to implement a solution?

> Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.

Surely there's a better way than manually counting each character?

def canConstruct(self, ransomNote: str, magazine: str) -> bool:
    c1, c2 = Counter(ransomNote), Counter(magazine)
    for letter in c1:
        if not (letter in c2 and c2[letter] >= c1[letter]):
            return False
        
    return True

答案1

得分: 2

# 确实如此!计数器的减法将产生一个新的计数器对象,其中仅包含具有正值的键。如果一个项是另一个项的子集,超集的减法将产生一个空字典。这意味着以下函数可以确定任何(可散列的)"集合",包括字符串,是否是另一个的子集
def isSubset(self, subset, superset) -> bool:
    return not Counter(subset) - Counter(superset)

# 在这种情况下,以下一行代码就足够了
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
    return not Counter(ransomNote) - Counter(magazine)
英文:

Indeed there is! Counter subtraction will yield a new Counter object containing only keys with positive values. If one item is a subset of another, the subtraction of the superset will yield an empty dictionary. That means that the following function can determine if any (hashable) "collection," including strings, is a subset of another

def isSubset(self, subset, superset) -> bool:
    return not Counter(subset) - Counter(superet)

In this case, the following one-liner would do

def canConstruct(self, ransomNote: str, magazine: str) -> bool:
    return not Counter(ransomNote) - Counter(magazine)

答案2

得分: 1

另一种 Counter 方法

from collections import Counter
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        a, b = Counter(ransomNote), Counter(magazine)
        return (a & b) == a
        # or return a.__iadd__(b).__eq__(a)
英文:

Another Counter approach

from collections import Counter
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        a, b = Counter(ransomNote), Counter(magazine)
        return (a & b) == a
        # or return a.__iadd__(b).__eq__(a)

答案3

得分: 0

def canConstruct(self, ransomNote: str, magazine: str) -> bool:
return Counter(ransomNote) <= Counter(magazine)

Submitted at LeetCode, got accepted.

英文:

Simple submultiset check:

def canConstruct(self, ransomNote: str, magazine: str) -&gt; bool:
    return Counter(ransomNote) &lt;= Counter(magazine)

Submitted at LeetCode, got accepted.

huangapple
  • 本文由 发表于 2023年2月18日 07:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定