Eric W.
Eric W.

Reputation: 3

Comparing two maps to see if one is a subset

I am checking two maps of that is case sensitive and if duplicates occur in the note_map the mag_map must be greater than or equal.

void checkMagazine(vector<string> magazine, vector<string> note) {
    std::map<string, int> mag_map;
    std::map<string, int> note_map;

    for(auto it : magazine){
       mag_map[it]++;
    }

     for(auto it : note){
       note_map[it]++;
    }

I would like to check mag_map to see if it holds the keys in note_map and I also want to see if there are enough occurrences in the mag_map to make the note.

Upvotes: 0

Views: 105

Answers (1)

David G
David G

Reputation: 96810

You're off to a good start. Now you just need to iterate through note and check if each word is present in mag_map. If it is, then you also need to check if the word occurs at least as many times in mag_map as it does in note_map. If at any iteration one of the conditions is not true, then you can break from there and print "No", because the ransom note cannot be made from the magazine.

Hint: use find(Key) to search and operator[] to get the count.

Upvotes: 1

Related Questions