383 - Ransom Note

Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.

Example 1:
Input: ransomNote = "aa", magazine = "aab"
Output: true

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map <char,int> map; // key: letter, value: count
        for ( int i = 0; i < magazine.size(); i++ ) {
            map[magazine[i]]++;
        }

        for ( int i = 0; i < ransomNote.size(); i++ ) {
            auto it = map.find( ransomNote[i] );
            if ( it != map.end() ) {
                it->second--;
                if ( it->second == 0 ) map.erase(it);
            }
            else {
                return false;
            }
        }

        return true;
    }
};