Search
 
SCRIPT & CODE EXAMPLE
 

CPP

what do you mean by smallest anagram of a string

#include<iostream>
#include<algorithm>

using namespace std;

int main(){
string s,p;
int t;
cin>>t;
while(t--){
int j=0,i=0;
cin>>s>>p;

 while (i<s.length())
 {
    if(p[j]==s[i]){
    s.erase(s.begin()+i);
    j++;
    i=0;
    
  
    

  }
  else
  {
    i++;
  }
  
   
 }
 

int k=-1;
sort(s.begin(),s.end());
for (int i = 0; i < s.length(); i++)
{
  if(p[0]>=s[i])
  k=i;
}
s.insert(k+1,p);
cout<<s<<endl;
}
return 0;
}
Comment

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;
   }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: string reverse iterator c++ 
Cpp :: vector size 
Cpp :: convert std vector to array 
Cpp :: c++ for each loop 
Cpp :: exponent power of x using c c++ 
Cpp :: How to split a string by Specific Delimiter in C/C++ 
Cpp :: il2cpp stuck unity 
Cpp :: changing values of mat in opencv c++ 
Cpp :: how to check char array equality in C++ 
Cpp :: find text in string c++ true false 
Cpp :: clear previous terminal output c++ 
Cpp :: how to remove the scroll bar in pyqt6 
Cpp :: how to check if vector is ordered c++ 
Cpp :: power in c++ 
Cpp :: c++ json parser euc-kr 
Cpp :: what is the meaning of life and everything in the universe 
Cpp :: Reverse a linked list geeksforgeeks in c++ 
Cpp :: vectors in c++ 
Cpp :: what is throw in c++ 
Cpp :: c++ - 
Cpp :: #define in cpp 
Cpp :: Temparory Email Id 
Cpp :: how to sort string array in c++ 
Cpp :: C++ Vector Operation Change Elements 
Cpp :: C++ switch..case Statement 
Cpp :: vector of vectors c++ 
Cpp :: bool nullable to bool c# 
Cpp :: c++ throw index out of bound 
Cpp :: COs trigonometric function 
Cpp :: whatsup 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =