View Code of Problem 2591

#include<iostream>
#include<unordered_map>
#include<algorithm>
using namespace std;

	int main()
	{
		int t;
		cin >> t;
		while(t --)
		{
			string str;
			unordered_map<char,int> hash; 
			string s1, s2;
			cin >> s1 >> s2;
			for(auto s : s1)
			{
				hash[s] ++;
			}
			sort(s2.begin(),s2.end());
			for(auto s : s2)
			{
				if(hash[s] > 0)
				{
					str += s;
					hash[s] --;
				}
			}
			if(str == "") cout << "-1" << endl;
			else cout << str << endl;
		}
		return 0; 
	}

Double click to view unformatted code.


Back to problem 2591