Skip to content

Latest commit

 

History

History
26 lines (24 loc) · 690 Bytes

049.md

File metadata and controls

26 lines (24 loc) · 690 Bytes

49. Group Anagrams

Solution 1

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        map<string, vector<string>> ans;
        for (int i = 0; i < strs.size(); i++) {
            string key = strs[i];
            sort(key.begin(), key.end());
            if (ans.find(key) == ans.end()) {
                vector<string> cur;
                ans[key] = cur;
            }
            ans[key].push_back(strs[i]);
        }
        vector<vector<string>> ret;
        map<string, vector<string>>::iterator it;
        for (it = ans.begin(); it != ans.end(); it++)
            ret.push_back(it->second);
        return ret;
    }
};