Write by myself in c++


  • 0
    T

    class Solution {

    public:

    vector<vector<string>> groupAnagrams(vector<string>& strs) 
    {
        map<string,int>mapint;
        map<string,int>::iterator iter;
        vector<vector<string>> result(0,vector<string>(0));
        int m=0;
        int num=0;
        string aa;
        for(int i=0;i<strs.size();i++)
        {
             aa=strs[i];
             sort(strs[i].begin(),strs[i].end());
            if((iter=mapint.find(strs[i]))!=mapint.end())
            {
                m=iter->second;
               result[m].push_back(aa);
            }
            else
            {
                mapint.insert(pair<string,int>(strs[i],num));
                result.push_back(vector<string>(0));
                result[num].push_back(aa);
                num++;
            }
        }
        for(int j=0;j<num-1;j++)
        {
            sort(result[j].begin(),result[j].end());
        }
        return result;
    }
    

    };


Log in to reply
 

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