1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <string> #include <unordered_map> using std::string; using std::unordered_map;
class Solution { public: bool canConstruct(string ransomNote, string magazine) { unordered_map<char, int> uset; for (auto &ch : magazine) uset[ch]++;
for (auto &ch : ransomNote) { if (!uset.count(ch)) return false;
if (uset[ch] == 0) return false; uset[ch]--; } return true; } };
|