


Shrey has just arrived in the city. When he entered the city, he was given two strings. Now, after arriving at his college, his professor gave him an extra string. To check his intelligence, his professor told him to check if the third string given to him has all the characters of the first and second strings in any order. Help Shrey before his professor scolds him. He has to answer “YES” if all characters are present else “NO”.
Example: ‘HELLO’ and ‘SHREY’ are two initial strings, and his professor gave him ’HLOHEELSRY’. So, here all the characters are present, so he has to say “YES”.
Note: The strings contain only uppercase Latin characters.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains three strings, and the first two strings, i.e. ‘FIRST’ and ‘SECOND’ are the strings which he was given at the beginning, and the third string is ‘THIRD’, which was given by the professor.
Output Format:
For each test case, you have to return “YES” if all characters are present in the third string of the first and second strings; else, return “NO”.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
1 <= T <= 10^2
1 <= |FIRST|, |SECOND|, |THIRD| <= 10^5
Time Limit: 1 sec
2
HI HEY EIHYH
ALL GOOD ADOLLG
YES
NO
In the first test case, the string ‘THIRD’ has all the characters present in the strings ‘FIRST’ and ‘SECOND’. So, we will return “YES”.
In the second test case, the strings ‘FIRST’ and ‘SECOND’ combined has 1 A, 2 L, 1 G, 2 O and 1 D. While the string ‘THIRD’ has 1 A, 2 L, 1 G, 1 O and 1 D and So, it has one character less than the combined ‘FIRST’ and ‘SECOND’. Thus, we will return “NO”.
2
CODING NINJA NINCODINGJA
YES NO NEEOOYS
YES
NO
In the first test case, the string ‘THIRD’ has all the characters present in the strings ‘FIRST’ and ‘SECOND’. So, we will return “YES”.
In the second test case, the strings ‘FIRST’ and ‘SECOND’ combined have 1 N, 1 Y, 1 E, 1 S and 1 O. While the string ‘THIRD’ has 1 N, 1 Y, 2 E, 1 S and 2 O and So, it has one character more than ‘FIRST’ and ‘SECOND’. Thus, we will return “NO”.
Can you think of iterating entirely in strings ‘FIRST’ and ‘SECOND’ and erasing the first occurrence of that character from ‘THIRD’?
The basic idea of this approach is that we will initially iterate through both the strings (‘FIRST’ and ‘SECOND’) and for all the characters present in them we will try to remove the same character from ‘THIRD’. So, after every iteration, one character from string ‘THIRD’ will be removed and if the letter is not found in between then we will return “NO”. At last, we will check that if string ‘THIRD’ becomes empty that means that all characters match and will return “YES” or else will return “NO” as few extra characters are present in string ‘THIRD’.
Here is the algorithm:
O(N^2), where N is the maximum length of the string from ‘FIRST’ and ‘SECOND’.
Because in the worst case we had to check for the occurrence of the character and that character is present at the end of the string in ‘THIRD’. So, we have to iterate through the entire string for all the characters. Thus, the time complexity is O(N^2).
O(N), where N is the maximum length of the string from ‘FIRST’ and ‘SECOND’.
Because we are using any extra array for keeping track of the visited position of string ‘THIRD’.