Recognize the string..pls chech the code..not printing correct ans

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

string recognizes(string s)
{
unordered_map<char,int> mp;
for(int i=0; i<s.size(); i++)
{
mp[s[i]]++;
}

    priority_queue<pair<int,char>> q;
    for(auto x:mp)
    {
        q.push({x.second,x.first});
    }
    
    string ans="";
    while(!q.empty())
    {
        pair<int,char> temp=q.top();
        ans+=temp.second;
        temp.first--;
        q.pop();
        if(!q.empty())
        {
            pair<int,char> temp2=q.top();
            ans+=temp2.second;
            temp2.first--;
            q.pop();
            if(temp2.first>0)
                q.push(temp2);
            if(temp.first>0)
                q.push(temp);
        }
        else
        {
            if(temp.first!=0)
                return "";
        }
    }
    return ans;

}
int main()
{
string s;
getline(cin,s);
string res=recognizes(s);
cout<<res<<endl;
return 0;
}

hi @divisharoy14_5d0aa86c37c6588a refer


ive commented