You are viewing a single comment's thread. Return to all comments →
Yeah, I think this is the best approach as it only needs a single Hash Table, as opposed to the Problem Setter's solution that needed two Hash Tables.
As a further improvement, the two string vectors could be also removed and the magazine's words directly read into the Hash Table:
int main() { int m, n; unordered_map<string, int> magazine; string word; cin >> m >> n; /* Read magazine. */ for (int i = 0; i < m; ++i) { cin >> word; magazine[word]++; // (zero) value-initialized } /* Read ransom note. */ for (int j = 0; j < n; ++j) { cin >> word; if (--magazine[word] < 0) { // (zero) value-initialized cout << "No" << endl; return 0; } } cout << "Yes" << endl; return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
Yeah, I think this is the best approach as it only needs a single Hash Table, as opposed to the Problem Setter's solution that needed two Hash Tables.
As a further improvement, the two string vectors could be also removed and the magazine's words directly read into the Hash Table: