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;
}
3
3
#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;
}
}
Thank you!
3
0
Are there any code examples left?
New code examples in category C++
-
C++ 2023-04-28 17:44:25
-
C++ 2022-03-27 19:20:39 lists occurrences of characters in the string c++
-
C++ 2022-03-27 18:00:14 variabili in c++
-
C++ 2022-03-27 17:10:08 repeat character n times c++
-
C++ 2022-03-27 15:50:07 delete an array c++
-
C++ 2022-03-27 15:40:12 C++ pointer to base class
-
C++ 2022-03-27 12:15:21 find the graph is minimal spanig tree or not
-
C++ 2022-03-27 11:30:15 multi variable assignment cpp
-
C++ 2022-03-27 11:05:17 c++ pi float
-
C++ 2022-03-27 10:20:12 why exceptions can lead to memory leaks