Amazing Strings

Posted: 25 Nov, 2020
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

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
Approach 1

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”.
Try Problem