You are given a list of lottery tickets ‘tickets’ where each ticket is a string of lowercase characters. You are also given a match string, ‘matchStr’. A ticket will be considered a winning ticket if a substring of ‘matchStr’ is equal to the ticket by skipping at most 'K' characters of the substring.
To make more winning tickets, you can perform the following operations-:
1- Change 'a' to 'o' or vice versa
2- Change 't' to 'l' or vice versa
Your task is to find the number of winning tickets.
For example:You are given ‘tickets’ = [‘abcd’, ‘acmfgtld’], ‘K’ = 2, and 'matchStr' = ‘aabacd’
For the ticket ‘abcd’, is equal to the substring ‘abacd’ by skipping the third character of ‘abacd’. Hence the answer is 1.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of input contains two space-separated integers, ‘N’ and ‘K’, representing the number of tickets and the given integer.
The second line of each test case contains a single string, representing the match string ‘matchStr’.
The next line contains ‘N’ space-separated strings representing the ticket strings.
Output Format:
For each test case, print a single integer representing the number of tickets that can be the winning ticket.
Print a separate line for each test case.
1 <= T <= 10
1 <= N, K <= 10^3
1 <= |tickets[i]| , |matchStr| <= 500
Time Limit: 1 sec
2
2 2
aabacd
abcd acmfgtld
3 1
akgfhdlskjes
agfh aghd kje
1
2
For the first test case, ‘tickets’ = [‘abcd’, ‘acmfgtld’], ‘K’ = 2, and 'matchStr' = ‘aabacd’
For the ticket ‘abcd’, is equal to the substring ‘abacd’ by skipping the third character of ‘abacd’.
Hence the answer is 1.
For the second test case, ‘tickets’ = [‘agfh’, ‘aghd’, ‘kje’ ], ‘K’ = 1, and 'matchStr' = ‘akgfhdlskjes’
For the ticket ‘agfh’, is equal to the substring ‘akgfh’ by skipping the second character of ‘akgfh’.
For the ticket ‘kje’, is equal to the substring ‘kje’ without skipping any character of the substring.
Hence the answer is 2.
2
6 2
stjqiefjodjuxyge
tjqe juye xyg ojux qejo ji
19 3
hawesqvddegyly
l s vd wes h qvd vde aw egl svd vde y de s a d w yly h
6
19
Try the brute force approach by traversing through each substring.
In this approach, we will iterate through every substring and check if any ticket in matches with the substring. If a ticket is matched, we insert it in a set to stop a ticket from being added twice.
We will create a matchChar(char1, char2) method that will check if the characters char1 and char2 are matching or not.
We will also create a function match(matchStr, ticket), which matches a matchStr with the ticket with the given conditions
Algorithm:
O(M^2*(N)), where N and M are the total lengths of all tickets and the length of the match string.
We are iterating through all the substrings of the match string which will cost O(M^2) time. For each substring, we are matching it with every ticket. Hence the overall time complexity is O(M^2*(N)).
O(1).
Constant space is being used. Hence the overall space complexity is O(1).