49. Group Anagrams

https://leetcode.com/problems/group-anagrams/description/

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,multiset<string>> m;
        for (auto s : strs) {
            string ss = s;
            sort(ss.begin(), ss.end());
            m[ss].insert(s);
        }
        vector<vector<string>> res;
        for (const auto& p : m) {
            vector<string> v;
            for (const auto& s : p.second)
                v.push_back(s);
            res.push_back(v);
        }
        return res;
    }
};

 

posted @ 2018-05-19 12:56  JTechRoad  阅读(77)  评论(0编辑  收藏  举报