Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 679 Bytes

383. Ransom Note.md

File metadata and controls

30 lines (23 loc) · 679 Bytes

Leetcode 383. Ransom Note

Hash Table, Counting

Runtime: 96 ms, faster than 73.16% of Python3

Memory Usage: 14.1 MB, less than 66.60% of Python3

# Time Complexity - O(n)

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        m = {}
                
        for s in magazine:
            try:
                m[s] += 1
            except Exception:
                m[s] = 1
                
        
        for s in ransomNote:
            try:
                m[s] -= 1
                if m[s] == 0:
                    del m[s]
            except Exception:
                return False
        
        return True