

Consider P = "a*d" and Q = "abcd"
If we replace '*' with "bc" the resulting string P becomes "abcd" which is equal to Q. Hence, the string P is compatible with String Q.
The first line of input contains an integer ‘T’ which contains the number of test cases.
The first line of each test case contains an integer 'N' denoting the number of strings in the array 'ARR'.
The second line of each test case contains 'N' space separated strings denoting the array 'ARR'.
The third line of each test case contains the string 'S'.
For each test case, print 1 if any such string exists in the array 'ARR' that string 'S' is compatible with. Otherwise print 0.
You do not need to print anything. It has already been taken care of, just implement the given function.
1 <= T <= 10
1 <= N <= 10
1 <= |S| <= 100
1 <= |M| <= 100
Strings 'S' contains lowercase English letters and '*' only.
String 'M' contains lowercase English letters only.
Where 'M' denotes any element of the array 'ARR', |S| and |M| denotes the length of String 'S' and String 'M' respectively.
Time Limit: 1 sec
First of all, let's define a function checkCompatibility to check compatibility between two strings 'P' and 'Q' which takes 4 arguments the string 'P' the string 'Q', and 2 integer values ‘i’ and ‘j’ to keep track of the index of 'P' and 'Q' respectively.
The idea is pretty straightforward: iterate through 'P' and 'Q' while there is a match between the current character of 'P' and the current character of 'Q'. If we reach the end of both strings while there is still a match, we will return 1 denoting that the strings are compatible, otherwise, we will return 0. The scan is done by having a pointer each in string 'P' and 'Q'.
Below is the detailed algorithm:
Now we will iterate over the Array ‘ARR’ and call checkCompatibility ( ‘S’, ‘M’, 0, 0 ) for each of the array element ‘M’ and we will return 1 if we get 1 as output during any iteration. Otherwise we will return 0.
In the previous approach, the function checkCompatibility recursively checks the compatibility between two strings which has many overlapping subproblems. i.e. a recursive function computes the same sub-problems again and again. So we can use Dynamic Programming to overcome the overlapping subproblems.
Note that the remaining work will be the same as the previous approach, we will just optimize the ‘checkCompatibility’ function using Dynamic Programming.
Steps:
The idea is to build an iterative approach to check compatibility between two strings. The benefit of an iterative approach is that only constant extra space will be used. We will use four-pointers ‘i’ pointing to String 'P', ‘j’ pointing to String Q, 'LAST_STAR_INDEX' to store the latest index of String 'P' at which the character is '*' and 'CORRESPONDING_INDEX' to store the corresponding Index of string 'Q' for that '*' character. The approach is to start comparing both the strings character by character and backtrack to the latest position containing '*' whenever a match is not found. In the end if we reach the end of both strings we will return 1 otherwise we will return 0.
Note that the remaining working will be the same as the first approach, we will just optimize the function ‘checkCompatibility’.
Below is the detailed algorithm:
Return 1 if ‘j’ = 'P'.length() otherwise return 0.