#include <iostream>
#include<string.h>
using namespace std;
string smallestKMP(string s,string pat){
string res="";
int dict[26];
bool flag=false;
memset(dict,0,sizeof(dict));
if(s.size()<pat.size() || s.size()==0){
return res;
}
for(int i=0;i<s.size();i++){
dict[s[i]-'a']++;
}
for(int i=0;i<pat.size();i++){
dict[pat[i]-'a']--;
}
for(int i=0;i<26;i++){
if((i+'a')<=pat[i] || flag){
while(dict[i]){
res+=i+'a';
dict[i]--;
}
}
else{
res+=pat;
flag=true;
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s,pat;
int t;
cin>>t;
cin>>ws;
while(t--){
getline(cin,s);
getline(cin,pat);
cout<<smallestKMP(s,pat)<<"\n";
}
return 0;
}
I tried this:
1
aabbcc
cbba
Output: acbba
Correct answer: acbbac
I dont think the last for loop is correct because after flag = true will it still add chars to res?