Help me in solving SUBANAGR problem

My issue

include <bits/stdc++.h>
using namespace std;

int main() {
int n;
cin >> n;

unordered_map<char, int> mp;

for (int i = 0; i <= n; i++) {
    string str;
    cin >> str;

    unordered_set<char> st;

    for (int j = 0; j < str.size(); j++) st.insert(str[j]);

    for (auto it : st) mp[it]++;
}

string ans;

for (auto it : mp) if (it.second == n) ans.push_back(it.first);

if (ans.size() == 0) ans = "no such string";
else sort(ans.begin(),ans.end());
cout << ans << endl;

return 0;

}

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    unordered_map<char, int> mp;

    for (int i = 0; i <= n; i++) {
        string str;
        cin >> str;

        unordered_set<char> st;

        for (int j = 0; j < str.size(); j++) st.insert(str[j]);

        for (auto it : st) mp[it]++;
    }

    string ans;
   
    for (auto it : mp) if (it.second == n) ans.push_back(it.first);

    if (ans.size() == 0) ans = "no such string";
    else sort(ans.begin(),ans.end());
    cout << ans << endl;

    return 0;
}

Problem Link: Subanagrams Practice Coding Problem

@shyampandey_16
here plzz refer my c++ code for better understanding of the logic

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int n;
	cin>>n;
	string s[n];
	int freq[n][26];
	memset(freq,0,sizeof(freq));
	for(int i=0;i<n;i++)
	{
	    cin>>s[i];
	    sort(s[i].begin(),s[i].end());
	    for(int j=0;j<s[i].size();j++)
	    {
	        freq[i][s[i][j]-'a']++;
	    }
	}
	string ans;
	for(int i=0;i<s[0].size();i++)
	{
	    int ch=0;
	    for(int j=1;j<n;j++)
	    {
	        if(freq[j][s[0][i]-'a'])
	        {
	            freq[j][s[0][i]-'a']--;
	        }
	        else
	        {
	            ch=1;
	            break;
	        }
	    }
	    if(!ch)
	    ans+=s[0][i];
	}
	if(ans.size()==0)
	cout<<"no such string";
	else
	cout<<ans;
	return 0;
}