Update appNew update is available. Click here to update.
Last Updated: 25 Nov, 2020
Amazing Strings
Easy
Problem statement

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.

Input Format:
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.
Constraints:
1 <= T <= 10^2
1 <= |FIRST|, |SECOND|, |THIRD| <= 10^5

Time Limit: 1 sec
Approaches

01Approach

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:
 

  1. Declaring an array of size equal to length of the string β€˜THIRD’ and assigning them value as 0.
  2. Iterating from starting of string β€˜FIRST’ to ending of it.
    • Declare a temporary variable named β€˜TEMP’ to keep track of the current character and initialize it with 0.
    • Check for the first occurrence of that particular character in the string β€˜THIRD’.
      • If the character matches the current character of β€˜FIRST’ and also the current character is not visited them we got the corresponding character for the current character.
        • Mark current index as visited and change of β€˜TEMP’ variable to 1 as it indicates that corresponding character is found and break the loop.
    • If β€˜TEMP’ is still 0 that means that the corresponding character is not found and so, return β€œNO”.
  3. Iterating from starting of string β€˜SECOND’ to ending of it.
    • Declare a temporary variable named β€˜TEMP’ to keep track of the current character and initialize it with 0.
    • Check for the first occurrence of that particular character in the string β€˜THIRD’.
      • If the character matches the current character of β€˜SECOND’ and also the current character is not visited them we got the corresponding character for the current character.
        • Mark current index as visited and change of β€˜TEMP’ variable to 1 as it indicates that corresponding character is found and break the loop.
    • If β€˜TEMP’ is still 0 that means that the corresponding character is not found and so, return β€œNO”.
  4. After iterating both the strings β€˜FIRST’ and β€˜SECOND’, we will check that if any character is still unvisited or not.
  5. If any character is found not visited then we will return β€œNO”, else β€œYES”.