View Code of Problem 2591

#include <bits/stdc++.h>
#define N 1000
using namespace std;
int main() 
{	
	int t;
	cin>>t;
	while(t--)
	{
		string str1,str2;
		cin>>str1>>str2;
		int k=0;
		char ans[100010];
		int hashtable[256];
		fill(hashtable,hashtable+256,0);
		for(int i=0;i<str1.size();i++)
			hashtable[str1[i]-'A']++;
		for(int i=0;i<str2.size();i++)
		{
			if(hashtable[str2[i]-'A']>0)
			{
				ans[k++]=str2[i];
				hashtable[str2[i]-'A']--;
			}
		}
		ans[k]='\0';
		sort(ans,ans+k);
		if(k==0)
			cout<<"-1";
		else
			cout<<ans;
		if(t>0)
			cout<<endl;
	}
}

Double click to view unformatted code.


Back to problem 2591