Ninja has been given a string âTEXTâ and an array/list of strings âWORDSâ of size âNâ. Ninja has to print all index pairs (âiâ, âjâ) such that the substring of âTEXTâ is present in the âWORDSâ.
Note:Ninja has to return all the index pairs (âiâ, âjâ) in sorted order, i.e., sort them by the first element of the pair, i.e., âiâ.
The first line contains a single integer âTâ representing the number of test cases.
The first line of each test case will contain a single space-separated string âTEXTâ which represents the value of âTEXTâ.
The next line of each test case contains a single space-separated integer âNâ denoting the size of âWORDSâ.
The next line of each test case contains âNâ space-separated integers representing the values of âWORDSâ.
Output Format:
For each test case, print all index pairs (âiâ, âjâ) such that the substring of âTEXTâ is present in the âWORDSâ in sorted order according to the first element of the pair.
Output for every test case will be printed in a separate line.
Note:
You donât need to print anything; It has already been taken care of.
1 <= âTâ <= 50
1 <= | âTEXTâ | <= 100
1 <= âNâ <= 20
1 <= |âWORDS[i]â | <= 50
Where âTâ is the number of test cases, 'N' is the size of array/vector âWORDSâ, |âWORDS[i]â| represents the size of each word in âWORDSâ and |âTEXTâ| represents the size of input string âTEXTâ|.
Time limit: 1 sec
1
abcdefcd
2
cd abc
0 2
2 3
6 7
In the first test case:
For the string âcdâ the index of this substring in the given âTEXTâ string is (2, 3) and (6, 7).
For string âabcâ index of this substring in given âTEXTâ string is (0, 2).
Hence the sorted order of these indices is (0, 2), (2, 3), and (6, 7).
1
codingninjas
4
cod co c nar
0 0
0 1
0 2
In the first test case:
For string âcodâ index of this substring in given âTEXTâ string is (0, 2)
For string âcoâ index of this substring in given âTEXTâ string is (0, 1).
For string âcâ index of this substring in given âTEXTâ string is (0, 0)
For string ânarâ there is not an index present in this string âTEXTâ.
Hence the sorted order of these indices is (0, 0), (0, 1), and (0, 2).
Can you think of a simple iteration on âWORDSâ?
The basic idea is to first iterate on the âWORDSâ array/list and then for each word check in the string âTEXTâ whether this string is present or not. If the word is present then put all indexes in a list/vector âallIndexPairâ and at the end sort this list/vector and return this.
The steps are as follows:
For example:
âTEXTâ = âaaaâ
âWORDSâ = [a aa aaa]
For string âaâ indices are (0,0), (1,1), (2,2).
For string âaaâ indices are (0,1), (1,2).
For string âaaaâ indices are (0,2).
O(N^2 * log(N) + (N*L*X)), where âNâ is the size of âWORDSâ, âLâ is the length of âTEXTâ, and âXâ is the length of the longest word in âWORDSâ.
Because first, we are iterating over the âWORDSâ array/list and then check this word in the string âTEXTâ it takes O(N * L * X ) time. Then, we are sorting the âallIndexPairâ. In the worst-case size of âallIndexPairâ is (N*(N+1)) / 2 and therefore sorting takes O(N^2 * log(N)) time. Hence, the overall time complexity is O(N^2 * log(N) + (N*L*X)).
O(N^2), where âNâ is the size of âWORDSâ.
Because we are storing all the indices in âallIndexPairâ list/vector. Hence the space complexity is O(N^2).