At first I think of using two trie to save prefix and suffix. And save the index of word in the array to its trie node. But the space and the time needed is to big.
So I cut down one trie. Use only one trie, and in the trie, only save the common prefix and suffix.
Approach
Loop the array from the back, then for each string s, it only needs to search in the trie tree and then, you can find the answer string which index is greater than index of s.
Because there is only one trie tree, you only need to save the word of its common prefix and suffix. Eg. (“ababa”, you need to save “aba”, “a”, “ababa” to the trie tree.)
In order to find its common prefix and suffix, you use z-function.
After searching in trie tree and found the valid pairs, save current string(its common prefixs) to trie tree.