ransomNote and magazine consist of lowercase English letters.
Solutions
Solution 1: Hash Table or Array
We can use a hash table or an array \(cnt\) of length \(26\) to record the number of times each character appears in the string magazine. Then traverse the string ransomNote, for each character \(c\) in it, we decrease the number of \(c\) by \(1\) in \(cnt\). If the number of \(c\) is less than \(0\) after the decrease, it means that the number of \(c\) in magazine is not enough, so it cannot be composed of ransomNote, just return \(false\).
Otherwise, after the traversal, it means that each character in ransomNote can be found in magazine. Therefore, return \(true\).
The time complexity is \(O(m + n)\), and the space complexity is \(O(C)\). Where \(m\) and \(n\) are the lengths of the strings ransomNote and magazine respectively; and \(C\) is the size of the character set, which is \(26\) in this question.