Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
Hash Table String
有hash Table标签的似乎总要用到map类型
vector<string> anagrams(vector<string> &strs) {
//题目就没有看懂,不明白题目到底需要我求什么,经百度大致明白是要求有相同字符但顺序不同的单词,也就是说只要字符数量相同且每个字符出现的次数相同,即满足要求,参考了一下网上答案,这个题目题意不好理解
map<string, int> mp;
vector<string> result;
string s;
int len = strs.size();
for (int i = 0; i < len; ++i)
{
s = strs[i];
sort(s.begin(), s.end());
if (mp.find(s) == mp.end())
mp.insert(make_pair(s, i));
else
{
if (mp[s] >= 0)
{
result.push_back(strs[mp[s]]);
mp[s] = -1;
}
result.push_back(strs[i]);
}
}
return result;
}