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’.
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.
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
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).
The basic idea of this approach is to store all values of the ‘WORDS’ in a Trie ‘trie’.
Then we can find all the pairs of the given word by simply iterating on the ‘trie’.
The steps are as follows:
Divisible Substrings
Ninja and Numbers
Longest Palindromic Substring
Cakes
1-3 Palindrome