Leetcode049. Group Anagrams

//hashmap implement with STL
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
       // sort(strs.begin(),strs.end());         //sort all the element
        map<string,vector<string>>hashmap;
        for(vector<string>::iterator it=strs.begin();it!=strs.end();it++)
        {
            string str=*it;
            sort(str.begin(),str.end());
            hashmap[str].push_back(*it);     //hashmap;
        }
        vector<vector<string>>re;
        for(map<string,vector<string>>::iterator    it=hashmap.begin();it!=hashmap.end();it++)     //each group with the same key 
            re.push_back(it->second);
        return re;
    }
};

 

posted @ 2016-09-15 21:27  zeroArn  阅读(140)  评论(0编辑  收藏  举报