#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string> > res;
int sub = 0;
string tmp;
unordered_map<string,int> work;
for(auto str:strs) {
tmp = str;
sort(tmp.begin(),tmp.end());
if(work.count(tmp)) {
res[work[tmp]].push_back(str);
} else {
vector<string> vec(1,str);
res.push_back(vec);
work[tmp]=sub++;
}
}
return res;
}
};
先排序,再检查映射,若不存在,则新建一个vector并添加到其中,再将vector添加到res中