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;
    }
};

猜你喜欢

转载自www.cnblogs.com/JTechRoad/p/9060064.html
今日推荐