Skip to content

Latest commit

 

History

History

383-RansomNote

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Ransom Note

Problem can be found in here!

Solution: Hash Table

def canConstruct(ransomNote: str, magazine: str) -> bool:
    memo = defaultdict(int)
    for char in magazine:
        memo[char] += 1

    for char in ransomNote:
        if memo[char] == 0:
            return False

        memo[char] -= 1

    return True

Time Complexity: O(n+m), Space Complexity: O(1), where n is the length of ransomNote and m is the length of magazine.