Note: ‘JEWELS’ consist of distinct characters and ‘A’ and ‘a’ are treated as two different stone types.
For ExampleIf ‘STONES’ is “abAAc” and JEWELS is “Acd”.The number of jewels Ninja have is 3.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two integers, ‘N’ denoting the number of stones and M denoting the number of Jewels.
The second line of each test case contains an string of size N denoting ‘STONES’
The third line of each test case contains an string of size M denoting ‘JEWELS’
Output Format:
For each test case, print an single integer denoting the number of jewels Ninja found in the treasure box.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10000.
1 <= M <= 52
Time Limit = 1 sec
2
5 3
abAAc
Acd
4 2
acbd
Aa
3
1
For the first test case,
Ninja has 3 jewels, one ‘c’ and two ‘A’.Hence, the answer is 3.
For the second test case:
Ninja has only 1 jewel, one ‘a’.Hence, the answer is 1.
2
3 1
aaa
b
4 2
abcd
Bb
0
1
Check the JEWELS list for each stone.
In this approach, for each character in ‘STONES’ , we will iterate the ‘JEWELS’ string and check if the stone is a jewel or not. If we found a jewel, we will increment the ‘ANS’.
At last, we will return the ‘ANS’ corresponding to the number of jewels Ninja found.
Algorithm:
O(N*M), where ‘N’ is the number of stones and ‘M’ is the length of the ‘JEWELS’ string.
In this approach, we are checking the whole ‘JEWELS’ string for each character in the ‘STONES’ string, the total number of operations will be M*N. Hence, the overall time complexity is O(M*N).
O(1).
In this approach, we are using constant space. Hence, the overall space complexity is O(1).