Suppose I am given a string S.
I need to find the number of distinct substrings of S that contain S1 as the prefix and S2 as the suffix.
The range of S, S1 and S2 can be very large, that is, O(10^5).
For eg.
Suppose S is "abcdcd", S1 is "ab" and S2 is "cd".
The distinct substrings of "ababcdcd" are: "a", "b", "c", "d", "ab", "bc", "cd", "dc", "abc", "bcd", "cdc", "dcd", "abcd", "bcdc", "cdcd", "abcdc", "bcdcd", "abcdcd". The count of total distinct substrings can be easily found using Suffix Array. I am trying to extend the same idea to solve the question.
Out of these substrings, the substrings containing "ab" as prefix and "cd" as suffix are: "abcd", "abcdcd".
Thus the answer is 2.
PS: I believe it uses the Suffix Array but I am not sure how. Please help.