C++,use map to solve


  • 0
    B

    class Solution {
    public:
    bool isAnagram(string s, string t) {
    map<char,int> mapTempS;
    map<char,int> mapTempT;
    int leng1 = s.size();
    int leng2 = t.size();
    if(leng1 != leng2)
    return false;
    for(int i = 0;i<leng1;i++)
    {
    mapTempS[s.at(i)]++;
    }

             for(int j =0;j<leng2;j++)
             {
                 mapTempT[t.at(j)]++;
             }
           
             for(const auto &w : mapTempS)
             {
    
                 if( mapTempT[w.first] != w.second)
                     return false;
             }
             return true;
    }
    

    };


Log in to reply
 

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