Time Complexity = O ( M + N ). M = len ( magazine ), N = len ( ransomNote )
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
counter = Counter(magazine)
for r in ransomNote:
if r not in counter or counter[r] <= 0:
return False
else:
counter[r] -= 1
return True
No comments:
Post a Comment