Easy-to-understand Python solution (60ms, beats 100%)
- Go through each character in ransomNote.
- If the count of one character in ransomNote is larger than the count in magazine, return False.
- If all counts are less than or equal, return True.
Optimization: To avoid repeated checking for duplicate characters, we convert ransomNote into a set first.
for c in set(ransomNote): if ransomNote.count(c) > magazine.count(c): return False return True