1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { // Use sorted string as key std::unordered_map<string, vector<string>> anagramMap; for (string s : strs) { string tmp = s; std::sort(tmp.begin(), tmp.end()); anagramMap[tmp].push_back(s); } vector<vector<string>> ans; for (auto it : anagramMap) ans.push_back(it.second); return ans; } }; |