easy +10 pts

Ransom Note Builder

Can you construct the target message from available magazine words?

You are given two strings: `note` and `magazine`. Write a function `can_construct(note: str, magazine: str) -> bool` that returns `True` if the note can be constructed by using words from the magazine, or `False` otherwise. - Each word from the magazine can be used **at most once**. - You may use only lowercase letters (a-z) in the words; all input strings will contain only lowercase letters and spaces. - Words are separated by single spaces, and there are no leading or trailing spaces. - An empty note is always constructible. - The function should be case-sensitive, but since all input is lowercase, case is not an issue. Example: If `magazine = 'a b c'` and `note = 'a a'`, the answer is `False` because the magazine has only one 'a'. Implement the function exactly as specified.

Constraints

- Input strings may be up to 10^5 characters long. - The function should run in O(n + m) time, where n and m are the lengths of the note and magazine respectively.

Example

>>> can_construct('a b c', 'a b c')
True
>>> can_construct('a a', 'a b')
False
>>> can_construct('', 'anything')
True
>>> can_construct('a b', 'b a')
True
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Count word frequencies in both strings using dictionaries.
For each word in the note, ensure the magazine has at least as many occurrences.
If any word is missing or insufficient, return False; otherwise True.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.