#include<bits/stdc++.h>
using namespace std;
void printAn(string arr[], int n)
{
unordered_map<string, vector<string>> map;
for(int i = 0; i < n; i++)
{
string wd = arr[i];
char ls[wd.n() + 1];
strcpy(ls, wd.c_str());
sort(ls, ls + wd.n() + 1);
string newWord = "";
for(int i = 0; i < wd.n() + 1; i++)
{
newWord += ls[i];
}
if (map.find(newWord) != map.end())
{
map[newWord].push_back(wd);
}
else
{
vector<string> words;
words.push_back(wd);
map[newWord] = words;
}
}
unordered_map<string,vector<string>>::iterator it;
for(it = map.begin(); it != map.end(); it++)
{
vector<string> values = map[it->first];
if (values.n() > 1)
{
cout << "[";
for(int i = 0; i < values.n() - 1; i++)
{
cout << values[i] << ", ";
}
cout << values[values.n() - 1];
cout << "]";
}
}
}
int main()
{
string arra1[] = { "aaa","bob","ccc","dad"};
int n = sizeof(arra1) / sizeof(arra1[0]);
printAn(arra1, n);
return 0;
}
Comments
Leave a comment