您的位置:首页 > 其它

Anagrams

2015-07-11 01:28 232 查看
Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

Solution:

class Solution {
public:
vector<string> anagrams(vector<string>& strs) {
vector<string> res;
unordered_map<string, int> um;
for(int i = 0; i < strs.size(); ++i)
{
string str = strs[i];
sort(str.begin(), str.end());
if(um.count(str) == 0) um[str] = i;
else
{
if(um[str] >= 0)
{
res.push_back(strs[um[str]]);
um[str] = -1;
}
res.push_back(strs[i]);
}
}

return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: