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