Skip to content

Latest commit

 

History

History
51 lines (43 loc) · 1.07 KB

ransom-note.MD

File metadata and controls

51 lines (43 loc) · 1.07 KB

Hash Tables: Ransom Note (HackerRank)

https://www.hackerrank.com/challenges/ctci-ransom-note


// Complete the checkMagazine function below.
void checkMagazine(vector<string> magazine, vector<string> note) {
    multiset<string> mag_set;
    for (auto &word: magazine) {
        mag_set.insert(word);
    }

    for (auto &word: note) {
        auto curr = mag_set.find(word);
        if (curr != mag_set.end()) {
            mag_set.erase(curr);
        } else {
            cout << "No" << endl;
            return;
        }
    }

    cout << "Yes" << endl;
}

// Complete the checkMagazine function below.
void checkMagazine(vector<string> magazine, vector<string> note) {
    multiset<string> note_set;
    for (auto &word: note) {
        note_set.insert(word);
    }

    for (auto &word: magazine) {
        auto curr = note_set.find(word);
        if (curr != note_set.end()) {
            note_set.erase(curr);
        }

        if (note_set.empty()) {
            cout << "Yes" << endl;
            return;
        }
    }

    cout << "No" << endl;
}