B from A

4.3

16 votes
Algorithms, String Searching, String Algorithms
Problem

You are given two strings A and B of length N and M respectively consisting of lowercase English letters. You can make several strings using characters from A. If you use any character of A to make the string, that character will be removed from A.

That means if you select an integer i from 1 to the length of the string A and use Ai, then the character Ai will be removed and the string length gets reduced by 1, the indices of characters to the right of the deleted one also get reduced by 1.

Find the maximum number of strings you can make the same as that of B.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains two space-separated integers N and M.
  • The second line of each test case contains the string A.
  • The third line of each test case contains the string B.

Output format

For each test case, print the maximum number of strings you can make the same as that of B.

Constraints

1T101N,M105A and B contains lowercase English letters

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For test case 1: The first string uses characters at indexes 1, 3, and 4 (1-based indexing). Hence, the answer is 1.

For test case 2: The first string uses characters at indexes 1, 2, and 4 (1-based indexing). The second string uses characters at indexes 5, 6, and 7 (1-based indexing). Hence, the answer is 2.

Editor Image

?