Posted on

Description

submission

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        map<string, vector<string>> m;
        for(int i = 0; i < strs.size(); ++i) {
            string str = strs[i];
            sort(str.begin(), str.end());
            auto iter = m.find(str);
            if(iter == m.end()) {
                vector<string> sec{strs[i]};
                m.insert(make_pair(str, sec));
            } else {
                iter->second.push_back(strs[i]);
            }
        }
        vector<vector<string>> res;
        for_each(m.begin(), m.end(),
                [&res](const std::pair<string, vector<string>>& entry)
                 {
                     res.push_back(entry.second);
                 });
        return res;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *