Home | Projects | Notes > Problem Solving > LC - E - 242. Valid Anagram (undordered_map)
This solution uses STL unordered_map container.
Complexity Analysis:
s and t
unordered_map of size
Solution:
xxxxxxxxxx201class Solution {2public:3 bool isAnagram(string s, string t) {4 unordered_map<char, int> m;5
6 // add characters of the string s to the map m7 for (auto ch : s)8 m[ch]++;9
10 // remove characters of the string t from the map m11 for (auto ch : t)12 {13 if (--m[ch] == 0)14 m.erase(ch);15 }16
17 // if t is an anagram of s, m will be empty at this point18 return m.empty();19 }20};