class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
magazine = list(magazine)
for c in ransomNote:
if c not in magazine:
return False
else:
magazine.remove(c)
return TrueWhere and are the lengths of the strings and , respectively.
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
countR = Counter(ransomNote)
countM = Counter(magazine)
for c in countR:
if countM[c] < countR[c]:
return False
return TrueWhere and are the lengths of the strings and , respectively.
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
count = [0] * 26
for c in magazine:
count[ord(c) - ord('a')] += 1
for c in ransomNote:
count[ord(c) - ord('a')] -= 1
if count[ord(c) - ord('a')] < 0:
return False
return TrueWhere and are the lengths of the strings and , respectively.