You are given two strings 'S', and 'TARGET' of length 'N', and a string 'STAMP' of length 'M'. 'S' is initially made up of only character ‘#’. Your goal is to convert 'S' into 'TARGET' by stamping 'S' with 'STAMP' no more than 5 * 'N' times.
Stamping means takes a substring of 'S' of length 'M' and replace it with 'STAMP'.
Return the sequence of moves to convert 'S' into 'TARGET'. Each move represents the starting index of the substring we want to replace. If it is not possible to convert 'S' into 'TARGET' return an array containing only one element -1.
For example:
'S'= “#####” 'TARGET' = “ababbb” 'STAMP' = “ab”
Stamping substring starting at 4 , 'S' = “####ab”
Stamping substring starting at 3 , 'S' = “###abb”
Stamping substring starting at 2 , 'S' = “##abbb”
Stamping substring starting at 0 , 'S' = “ababbb”
Answer = [4, 3, 2, 0]
The first line of input contains an integer T’ denoting the number of test cases to run.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ denoting the length of 'TARGET' and 'STAMP' respectively.
The second line of each test case contains the string 'TARGET'.
The third line of each test case contains the string 'STAMP'.
Output Format:
For each test case, return the moves of size not greater than 5 * 'N', to convert 'S' into 'TARGET'.
Output for each test case is printed in a separate line.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N, M <= 1000
M <= N
TARGET and stamp have only lower case English characters.
Time Limit: 1 sec
2
6 4
aabcaa
abca
1 1
b
z
0 2 1
-1
In test case 1, after the first stamp, 'S' = “abca##”.
After the second stamp, 'S' = “ababca”.
After the third test stamp, 'S' = “aabcaa”.
In test case 2, it is not possible to convert 'S' into 'TARGET'.
2
3 2
Pqr
pq
2 2
mn
mn
-1
0
In test case 1, it is not possible to convert 'S' into 'TARGET'.
In test case 2, after the first stamp, 'S' = “mn”.
Can we do all the stamps in reverse order to convert ‘TARGET’ into a string of all ‘#’?
We will do the process in reverse. We will iterate over the substrings of ‘TARGET’ and try to convert them into ‘#’. Two characters of ‘TARGET’ and ‘STAMP’ matches if they are equal or the character in ‘TARGET’ is already replaced with a ‘#’.
We will maintain an array of indexes for each substring starting at ‘i’ which are not matching with character in ‘STAMP’. If some substring matches with ‘STAMP’ we will insert all its indexes in ‘STACK’.
We will pop an index from the ‘STACK’ and remove it from all the UNMATCHED array that contains it. If any of the UNMATCHED arrays becomes empty we will push it to the STACK and repeat the process.
The steps are as follows:
O(N * M), Where ‘N’, ‘M’ is the size of the ‘TARGET’ and the size of the ‘STAMP’.
Since there are N indexes to process and it takes O(M) time to process a single index. Thus the time complexity will be O(N * M).
O(N * M), Where ‘N’, ‘M’, is the size of the ‘TARGET’ and the size of the ‘STAMP’.
Since space is required to store unmatched indexes for each substring. Thus the space complexity will be O(N * M).