给一字符串数组, 将 错位词(指相同字符不同排列的字符串) 分组
样例
例1:
输入:
["eat","tea","tan","ate","nat","bat"]
输出:
[["ate","eat","tea"],
["bat"],
["nat","tan"]]
例2:
输入:
["eat","nowhere"]
输出:
[["eat"],
["nowhere"]]
注意事项
所有的输入均为小写字母
class Solution {
public:
/**
* @param strs: the given array of strings
* @return: The anagrams which have been divided into groups
*/
vector<vector<string>> groupAnagrams(vector<string> &strs) {
// write your code here
vector<vector<string>> ret;
int size = strs.size();
unordered_map<string,vector<string>> mymap;
for(int i = 0; i < size; i++)
{
//sort(strs[i].begin(), strs[i].end());
string copy = strs[i];
sort(copy.begin(), copy.end());
mymap[copy].push_back(strs[i]);
}
for(auto it = mymap.begin(); it != mymap.end(); it++)
{
vector<string> tmp;
for(int i = 0; i < (it->second).size(); i++)
{
tmp.push_back((it->second)[i]);
}
ret.push_back(tmp);
}
return ret;
}
};