49. Group Anagrams
Medium
2177134FavoriteShare
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
- All inputs will be in lowercase.
- The order of your output does not matter.
Accepted
426,583
Submissions
845,949
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
map<string,vector<string> > mp;
for(int i=0;i<strs.size();i++){
string temp=strs[i];
sort(temp.begin(),temp.end());//字符串排序之后应该一样
mp[temp].push_back(strs[i]);
}
vector<vector<string>> rt;
for(map<string,vector<string> >::iterator it=mp.begin();it!=mp.end();it++){
rt.push_back(it->second);
}
return rt;
}
};