给一字符串数组, 将 错位词(指相同字符不同排列的字符串) 分组
样例
例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
unordered_map<string,multiset<string>>mp;
for (int i = 0; i < strs.size(); i++) {
string t=Sort(strs[i]);
mp[t].insert(strs[i]);
}
vector<vector<string>> arr;
for(auto m : mp)
{
vector<string>tmp(m.second.begin(),m.second.end());
arr.push_back(tmp);
}
return arr;
}
string Sort(string s)
{
sort(s.begin(),s.end());
return s;
}
};