#include<bits/stdc++.h>
using namespace std;
vector<vector<string>> groupAnagrams(vector<string> &str)
{
map<string, vector<string>> anagramGroupMap;
//Iterating for anagram strings
for(auto st : str)
{
string sortedStr = st; //temp string
sort(sortedStr.begin(), sortedStr.end());
anagramGroupMap[sortedStr].push_back(st);
}
vector<vector<string>> result;
//Store the grouped anagrams in a vector<vector<string>>
for(auto &grp : anagramGroupMap)
{
result.push_back(grp.second);
}
return result;
}
int main()
{
vector<string> inputStr = {"eat","tea","tan","ate","nat","bat"};
vector<vector<string>> ans = groupAnagrams(inputStr);
for(const auto &grp : ans)
{
cout <<"[ ";
for(const string &ana : grp)
{
cout << ana << " ";
}
cout << " ]";
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgp2ZWN0b3I8dmVjdG9yPHN0cmluZz4+IGdyb3VwQW5hZ3JhbXModmVjdG9yPHN0cmluZz4gJnN0cikKewoJbWFwPHN0cmluZywgdmVjdG9yPHN0cmluZz4+IGFuYWdyYW1Hcm91cE1hcDsKCQoJLy9JdGVyYXRpbmcgZm9yIGFuYWdyYW0gc3RyaW5ncwoJZm9yKGF1dG8gc3QgOiBzdHIpCgl7CgkJc3RyaW5nIHNvcnRlZFN0ciA9IHN0OwkvL3RlbXAgc3RyaW5nCgkJc29ydChzb3J0ZWRTdHIuYmVnaW4oKSwgc29ydGVkU3RyLmVuZCgpKTsKCQlhbmFncmFtR3JvdXBNYXBbc29ydGVkU3RyXS5wdXNoX2JhY2soc3QpOwoJfQoJCgl2ZWN0b3I8dmVjdG9yPHN0cmluZz4+IHJlc3VsdDsKCgkvL1N0b3JlIHRoZSBncm91cGVkIGFuYWdyYW1zIGluIGEgdmVjdG9yPHZlY3RvcjxzdHJpbmc+PgoJZm9yKGF1dG8gJmdycCA6IGFuYWdyYW1Hcm91cE1hcCkKCXsKCQlyZXN1bHQucHVzaF9iYWNrKGdycC5zZWNvbmQpOwoJfQoJCglyZXR1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