Anagrams Together Quesiton of hashing testcase validation is not correct in Master Interview Questions for FAANG & Product Companies Course

Hi,

This is with reference to question Anagrams Together question of help. In the question, it is mentioned that the output order of the answer doesn’t matter but when I am running my testcases, it is failing just because of the order. That’s not right. For eg, the testcase on which my program is failing are

Input case
9
awe
wea
aew
eaw
ads
sad
mad
dam
adm

Output Expected by the platform:-

ads sad \n
awe wea aew eaw\n
mad dam adm

My output:-
mad dam adm \n
ads sad \n
awe wea aew eaw

While the answer is same, the order is not same which is why I haven’t awarded the full credit for the problem.
There should be some way to fix this

Please send your code

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include <unordered_map>
#include

using namespace std;

vector<vector> moveAnagramsTogether(vector arr, int len)
{
vector<vector> result;
map<string, vector> umap;
for(int i = 0; i < len; i++)
{
string str = arr[i];
sort(str.begin(), str.end());
if(umap.find(str) == umap.end())
{
vector temp = {arr[i]};
umap[str] = temp;
} else {
vector temp = umap[str];
temp.push_back(arr[i]);
umap[str] = temp;
}
}
for(auto itr = umap.begin(); itr != umap.end(); itr++)
{
result.push_back(itr->second);
}
return result;
}
int main(int argc, const char * argv[]) {
int n;
cin >> n;
vector arr;
string ele;
for(int i = 0; i < n; i++)
{
cin >> ele;
arr.push_back(ele);
}
vector<vector> result = moveAnagramsTogether(arr, n);
for(int i = 0; i < result.size(); i++)
{
vector current = result[i];
for(int j = 0; j < current.size(); j++)
{
cout << current[j] << " ";
}
cout << endl;
}
}

m
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include <unordered_map>
#include

using namespace std;

vector<vector> moveAnagramsTogether(vector arr, int len)
{
vector<vector> result;
map<string, vector> umap;
for(int i = 0; i < len; i++)
{
string str = arr[i];
sort(str.begin(), str.end());
if(umap.find(str) == umap.end())
{
vector temp = {arr[i]};
umap[str] = temp;
} else {
vector temp = umap[str];
temp.push_back(arr[i]);
umap[str] = temp;
}
}
for(auto itr = umap.begin(); itr != umap.end(); itr++)
{
result.push_back(itr->second);
}
return result;
}
int main(int argc, const char * argv[]) {
int n;
cin >> n;
vector arr;
string ele;
for(int i = 0; i < n; i++)
{
cin >> ele;
arr.push_back(ele);
}
vector<vector> result = moveAnagramsTogether(arr, n);
for(int i = 0; i < result.size(); i++)
{
vector current = result[i];
for(int j = 0; j < current.size(); j++)
{
cout << current[j] << " ";
}
cout << endl;
}
}

Hey I checked this question on Coding blocks server ,And yes the have a fix order of output
It’s implemented using Arraylist in java &I am not sure in which order its giving output .
So if your code is working on leetcode link provided then your code is correct.

I checked your code on LEETCODE,its working fine .

Thanks for the feedback.