1/19/2021

[LeetCode] 383. Ransom Note

Problem : https://leetcode.com/problems/ransom-note/

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