View Code of Problem 2591

#include<stdio.h>
#include<math.h>
#include<string.h>
//#include<algorithm>
#include <bits/stdc++.h>

using namespace std;
int num[1000001];
int main() {
	int T;
	scanf("%d",&T);
	char str[1000];
	char str2[1000];
	while(T--) {
		scanf("%s",str);
		scanf("%s",str2);
		int num[200]= {0};
		char res[1000];
		int j=0;
		for(int i=0; i<strlen(str); i++) {
			num[str[i]]++;
		}
		for(int i=0; i<strlen(str2); i++) {
			if(num[str2[i]]>0) {
				num[str2[i]]--;
				res[j]=str2[i];
				j++;
			}
		}
		if(j==0) {
			printf("-1\n");
		} else {
			sort(res,res+j);
			for(int i=0; i<j; i++) {
				printf("%c",res[i]);
			}
			printf("\n");
		}
	}
}

Double click to view unformatted code.


Back to problem 2591