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.
解题思路:
利用map的key-value特性,将满足key的值全部放入value中。
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string ,vector<string>> mp;
for(string str: strs)
{
string t = str;
sort(t.begin(), t.end());
mp[t].push_back(str);
}
vector<vector<string>> ret;
for(auto p :mp)
{
ret.push_back(p.second);
}
return ret;
}
};