Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

what do you mean by smallest anagram of a string

#include<bits/stdc++.h>
using namespace std;
int main(){
   int t;
   cin>>t;
   while(t--)
   {
       string s,p;
       cin>>s;
       cin>>p;

       map<char,int> ms,mp;
       for(int i=0;i<s.length();i++)
         ms[s[i]]++;
       for(int i=0;i<p.length();i++)
         mp[p[i]]++;
       for(auto it=mp.begin();it!=mp.end();it++)
       {
           ms[it->first]-=it->second;
       }
       string temp="";
       for(auto it=ms.begin();it!=ms.end();it++)
       {
           if(it->first!=p[0]){
           while(ms[it->first]>0)
           {
               temp+=it->first;
               ms[it->first]--;
           }
           
           }
           else
           {
               break;
           }
       }
       if(p[0]<p[1]){
       while(ms[p[0]]>0)
         {
           temp+=p[0];
           ms[p[0]]--;
         }
       }
       // cout<<temp;
      
       temp+=p;
       for(auto it=ms.begin();it!=ms.end();it++)
       {
           while(ms[it->first]>0){
              temp+=it->first;
              ms[it->first]--;}
       }
       cout<<temp<<endl;
   }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #smallest #anagram #string
ADD COMMENT
Topic
Name
6+8 =