Data Structures & Algorithms Hash Maps & Sets
💡
Exercise 30

Ransom Note 15 XP Easy

LeetCode Ctrl+Enter Run Ctrl+S Save

Given two strings ransomNote and magazine, return True if ransomNote can be constructed by using the letters from magazine.

Each letter in magazine can only be used once in ransomNote.

ransomNote = 'aa' magazine = 'aab' # True — magazine has 2 a's, we need 2 a's

The hash map approach: count all letters in magazine, then check if ransomNote's letters are available.

📋 Instructions
Implement `can_construct(ransomNote, magazine)` that returns True/False. Test with: - `can_construct('a', 'b')` → False - `can_construct('aa', 'aab')` → True Print both results.
Use a dictionary to count characters in magazine. Then iterate through ransomNote — for each char, check if count > 0 in the dict, and decrement.
🧪 Test Cases
Input
can_construct('a', 'b')
Expected
False
Boolean check
Input
can_construct('aa', 'aab')
Expected
True
Boolean check
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.