LC 0383 [E] Ransom Note - ALawliet/algorithms GitHub Wiki
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
mag = Counter(magazine)
found = 0
for c in ransomNote:
if c in mag:
if mag[c] >= 1:
found += 1
mag[c] -= 1
if mag[c] == 0:
mag.pop(c)
return found == len(ransomNote)
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
return not Counter(ransomNote) - Counter(magazine)
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
return Counter(ransomNote) & Counter(magazine) == Counter(ransomNote)