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.
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
if(strs.size()==0) return vector<vector<string> >();
vector<vector<string> > ret;
int len = strs.size();
string temp;
unordered_map<string, vector<string> > mp;
for(int i=0; i<len; i++)
{
temp = strs[i];
sort(temp.begin(), temp.end());
mp[temp].push_back(strs[i]);
}
unordered_map<string, vector<string> >::iterator mp_iterator = mp.begin();
for(; mp_iterator!=mp.end(); mp_iterator++)
{
ret.push_back(mp_iterator->second);
}
return ret;
}
};