This is a character availability problem. We have a supply of letters (the magazine) and a demand for letters (the ransom note). For every character the ransom note needs, the magazine must have at least that many copies available.
Each magazine letter can only be used once. So if the ransom note needs three 'a's, the magazine must contain at least three 'a's, and the same must hold for every other letter the note uses.
The question reduces to one comparison: does the character frequency of the ransom note fit within the character frequency of the magazine?
1 <= ransomNote.length, magazine.length <= 10^5 → With up to 100,000 characters, an O(m * n) brute force might be tight but an O(m + n) solution is comfortable.ransomNote and magazine consist of lowercase English letters → Only 26 possible characters. We can use a fixed-size array of length 26 instead of a hash map, giving O(1) space.Go through the ransom note letter by letter. For each letter, search the magazine for a matching letter. If a match exists, remove it so it can't be reused. If no match exists, the ransom note can't be constructed.
This mirrors physically cutting letters out of a magazine to compose a note: every letter you use is gone from the supply.
For every character in the ransom note, this scans the entire magazine to find a match. When both strings are close to 10^5 characters, that is up to 10^10 operations, well beyond what runs in time. Sorting both strings groups identical characters together, which removes the repeated scanning.
If we sort both strings, all identical characters are grouped together. We can then walk through both sorted strings with two pointers, advancing the magazine pointer until it catches up to each character the ransom note needs.
Because both strings are sorted, a character in the ransom note that is smaller than the current magazine character can never appear later in the magazine, so the magazine is missing it and the answer is false.
ransomNote and magazine by character.i for the ransom note, j for the magazine.ransomNote[i] == magazine[j], we've matched a character. Move both pointers forward.ransomNote[i] > magazine[j], the magazine character is too small. Move j forward to find a match.ransomNote[i] < magazine[j], the ransom note needs a character the magazine doesn't have. Return false.i reached the end), return true. Otherwise, return false.Sorting both strings costs O(m log m + n log n) before any comparison happens. The order of characters is irrelevant to the answer; only the count of each character matters. Those counts can be gathered in a single linear pass.
Since the only thing that matters is whether the magazine has enough of each letter, we can skip sorting. Count how many times each character appears in the magazine, then check the ransom note's demands against those counts.
Because the input is restricted to lowercase English letters, there are exactly 26 possible characters. An integer array of size 26 replaces a hash map here, which gives constant space and avoids hashing overhead.
The work happens in two passes: first count the supply (magazine characters), then check the demand (ransom note characters) against it.
The counting array acts as a ledger. Each magazine character adds to the balance for its letter, and each ransom note character withdraws one. Letters are independent, so the note can be built if and only if no letter is ever over-withdrawn. A balance that drops below zero means the note demanded more of that letter than the magazine supplied, so returning false on the first negative balance is both sufficient and complete.
count of size 26, initialized to zeros.count[c - 'a'].count[c - 'a'].