Why it works
- Every letter of the magazine can be used once, and where it stands does not matter. So all we need is how many of each letter the magazine has: count them first.
- The note spends letters: one off the count for each of its letters. A count below 0 means the note wants a letter the magazine has run out of: false at once.
- If the whole note is spent and nothing went below 0, every letter was there: true. Letters left in the magazine do not matter.
Complexity
Time O(m + n): one pass over the magazine and one over the note. Memory O(1): at most 26 counts for lowercase letters.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗