//字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
//示例 1:
//
//输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
//输出 : [["bat"], ["nat", "tan"], ["ate", "eat", "tea"]]
//示例 2 :
//输入 : strs = [""]
//输出 : [[""]]
//示例 3 :
//输入 : strs = ["a"]
//输出 : [["a"]]
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
vector<vector<string>> groupAnagrams(vector<string>& strs);
int main()
{
vector<string>arr(3);
for (int i = 0; i < 3; i++) {
cin >> arr[i];
}
groupAnagrams(arr);
return 0;
}
vector<vector<string>> groupAnagrams(vector<string>&strs) {
unordered_map<string, vector<string>> mp;
for (string& str : strs) {
string key = str;
sort(key.begin(), key.end());
mp[key].emplace_back(str);
}
vector<vector<string>> ans;
for (auto it = mp.begin(); it != mp.end(); ++it) {
ans.emplace_back(it->second);
for (const auto& str : it->second) {
cout << str << " ";
}
cout << endl;
}
return ans;
}
标签:str,strs,异位,字母,c++,vector,mp,key,include From: https://www.cnblogs.com/hellodjbk/p/18025618