Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
便利输入字符串,用unordered_map(类似hash_map)保存访问信息,最后便利map把anagram按组打印出来。
-------------------------------
vector<string> ret;
unordered_map<string, list<int>> vmap;
for (int i = 0; i < strs.size(); i++) {
string curs = strs[i];
sort(curs.begin(), curs.end());
vmap[curs].push_back(i);
}
for (auto it = vmap.begin(); it != vmap.end(); it++) {
if (it->second.size() < 2) continue;
for (auto lit = it->second.begin(); lit != it->second.end(); lit++) {
ret.push_back(strs[*lit]);
}
}
return ret;