The geeksforgeeks solution gives an efficient algorithm that accomplishes the following:
- Sorts the suffixes of the input string in lexicographic order.
- For every two adjacent suffixes in this order, finds the longest common prefix.
If you are after the number of distinct substrings of length $\ell$, you should proceed as follows:
- If the first suffix has length at least $\ell$, initialize your counter with 1, otherwise with 0.
- Now go over all the suffixes in lexicographic order. If the new suffix has length at least $\ell$ and the lcp with the preceding suffix has length smaller than $\ell$, then increment the counter.
For example, suppose that the string is ababa
, and you're after substrings of length $\ell = 3$. The ordered suffixes are a
, aba
, ababa
, ba
, baba
. The algorithm proceeds as follows:
- The first suffix
a
is shorter than $\ell$, so the counter starts at 0.
- The second suffix
aba
has length at least $\ell$, and the lcp with the preceding suffix has length less than $\ell$, so increment the counter. (We have discovered aba
.)
- The third suffix
ababa
has length at least $\ell$, but the lcp with the preceding suffix also has length at least $\ell$.
- The fourth suffix
ba
has length less than $\ell$.
- The fifth suffix
baba
has length at least $\ell$, and the lcp with the preceding suffix has length less than $\ell$, so increment the counter. (We have discovered bab
.)