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 26 27 28
| #include <string> #include <unordered_map> using std::string; using std::unordered_map;
class Solution { public: bool isAnagram(string s, string t) { if (s.size() != t.size()) { return false; } unordered_map<char, int> smap; for (char &ch : s) { smap[ch]++; } for (char &ch : t) { if (!smap.count(ch)) { return false; } smap[ch]--; if (smap[ch] < 0) { return false; } } return true; } };
|