55. Cut-Out Letters
A sender wants to build a note by cutting individual letters out of a magazine page. You are given the wanted text note and the printed text magazine, both made of lowercase letters. Each letter printed in the magazine can be cut out and used at most once.
Return true if every letter of note can be cut from a different printed letter of magazine, and false otherwise. The order of letters does not matter, only how many times each letter is needed compared to how many times it is printed. Use counting rather than searching.
Example 1
- Input:
- note = "cab", magazine = "abacus"
- Output:
- true
- Explanation:
The magazine prints c, a and b at least once each, so the note can be built.
Example 2
- Input:
- note = "zoo", magazine = "zebra"
- Output:
- false
- Explanation:
The note needs two o letters but the magazine prints none, so it cannot be built.
Constraints
1 ≤ note.length, magazine.length ≤ 105
Both strings contain only lowercase English letters
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n + m)
- Space
- O(1)