cpp solution with unorder_map


  • 1
    class Solution {
    public:
      bool isAnagram(string s, string t) {
        unordered_map<char, int> umap;
    
        for (auto i : s)
        {
          umap[i] ++;
        }
    
        for (auto j : t)
        {
          if (umap.find(j) != umap.end())
          {
    	umap[j] --;
          }
          else
          {
    	return false;
          }
        }
    
        for (unordered_map<char, int>::iterator iter = umap.begin(); iter != umap.end(); iter ++)
        {
          if (iter -> second > 0)
          {
    	return false;
          }
        }
        return true;
      }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.