You are given two strings STR1 and STR2. You need to check whether STR2 can be formed from the characters of STR1. Both the strings can contain any characters.
For example:If STR1 = “codingninjas” and STR2 = “sing”. We can form the second string using the characters of the first string. This is because “s”, “i”, “n”, “g” are present in the string STR1.
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains the first string STR1.
The second line of each test case contains the second string STR2.
Output Format:
For each test case, return “YES” if the second string can be formed from the characters of the first string. Otherwise, return ”NO”. (without the inverted commas)
Note:
You are not required to print the expected output, and it has already been taken care of. Just implement the function.
1 ≤ T ≤ 100
1 ≤ |STR1|, |STR2| ≤ 1000
Where 'T' is the number of test cases, |STR1| and |STR2| are the lengths of the strings STR1 and STR2 respectively.
Time Limit: 1 sec.
3
codingninjas
sing
good human
14good
coder
code
YES
NO
YES
The first test case has already been explained in the problem statement.
For the second test case, STR1 = “good human” and STR2 = “14good”. We cannot form the second string using the characters of the first string. This is because “1” and “4” are not present in the string STR1.
For the third test case, STR1 = “coder” and STR2 = “code. We can form the second string using the characters of the first string. This is because “c”, “o”, “d”, “e” are present in the string STR1.
3
madam
adam
H#LLO
hello
orange
orange
YES
NO
YES
Maintain count of characters.
O(N + M), where ‘N’ is the length of the first string and ‘M’ is the length of the second string.
Since we are traversing through both the strings.
O(1)
Since we are using a count array of constant size.