View Code of Problem 2591

#include<stdio.h>
#include<string.h>
int main() {
	int n, i, j, j1, a[26],f=0;
	char str1[100], str2[100];
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		for (j = 0; j < 26; j++) {
			a[j] = 0;
		}
		f = 0;
		scanf("%s %s", str1,str2);
		for (j = 0; j < strlen(str1); j++) {
			for (j1 = 0; j1 < strlen(str2); j1++) {
				if (str1[j] == str2[j1]) {
					f = 1;
					a[str2[j1] - 'A']++;
					str2[j1] = ' ';
					break;
				}
			}
		}
		if (f == 0) {
			printf("%d", -1);
		}
		else {
			for (j = 0; j < 26; j++) {
				while (a[j] != 0) {
					printf("%c", 'A' + j);
					a[j]--;
				}
			}
		}
		
		printf("\n");
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 2591