


You are given ‘WORDS’ = [“cat”, “mat”, “tac”]. Then the answer will be [(0, 2), (2, 0)}, because “cat” + “tac” = “cattac” which is a palindrome and “tac” + “cat” = “taccat” which is also a palindrome.
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains an integer ‘N’, representing the number of words in ‘WORDS’.
The next ‘N’ lines contain a String, representing a word of ‘WORDS’’.
For each test case, print all pairs of the distinct indices (i, j) in ‘WORDS’, such that the concatenation of WORDS[i] and WORDS[j] is a palindrome.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
2 <= N <= 1000
0 <= WORDS[i].LENGTH <= 100
WORDS[i] consists of lowercase English letters
Time Limit: 1 sec
In this approach, we will concatenate each word with every other word in the list and check if the concatenated word is a palindrome or not. If the concatenated word is a palindrome, we will add the indices of this pair to the resultant list. To iterate over every pair, we will use a nested for loop.
Algorithm :
In this approach, we will avoid checking the pairs, which will definitely not form a pair. We will check only the following cases:
Algorithm :
Maintain a function ‘ALLPALINDROMESUFFIXES’(‘WORD’):
Maintain a function ‘ALLPALINDROMEPREFIXES’(‘WORD’):
Prerequisite: Trie data structure.
In this approach, we will try to reduce the number of words to be checked for each word. We can observe that a word is a palindrome if the first and the last characters are the same, the second and second characters are the same, and so on. Using this property, we will reduce the number of words to be checked. For example, for the word ‘W’, starting with ‘a’, we only need to consider the words ending with ‘a’. This will reduce our search to words ending with ‘a’. If the second character of the word ‘W’ is ‘b’, then our search is further reduced to words ending with “ba”.
We will use a trie data structure for efficiently storing and searching words. Trie is an efficient data retrieval data structure mainly used for string manipulations. It provides a way to store strings efficiently and search for them in a lot lesser time complexity.
Each node of the trie will have three fields:
Now, we will arrange each word from the given list into this Trie, starting from its last character, and identify the node at the next layer by indexing into the root's next array with an index equal to the difference of the ending character and 'a'. We will create a new node if the indexed node is full. We will continue to the next layer towards the beginning of the word in this manner. When we are done with the word we will update the index of the root.
After building up the Trie, we will start our search.
Search(): In this method, we are going to iterate the word in the forward direction i.e, from 0th index to the last character, and check whether the root.index is greater than or equal to 0 or not. If it is greater than or equal to 0 and not minus -1(means that is the last character of the word) and it is not at the same index(because we should not combine it with itself) and is a palindrome then we will add the index of the current word and root.index to the result.
Algorithm :
Maintain a function ‘ADD’(‘ROOT’, ‘WORD’, ‘INDEX’):
Maintain a function ‘SEARCH’(‘WORDS’, ‘I’, ‘ROOT’, ‘RESULT’):