I need to design an algorithm in O (n) that given a string t of length n, calculate the number of different substrings of t.
Asked
Active
Viewed 201 times
-5
-
3and what is your approach? – Reinhard Männer Jan 06 '15 at 20:13
-
Formula is: **N!/(E0! ... Em!)** where E0 ... Em are the counts of the m distinct characters in the string. Seems pretty simple to take it from there in O(n). – Pieter Geerkens Jan 06 '15 at 20:15
1 Answers
0
Build a suffix automaton for the input string.
Find the sum of
maxLength[v] - maxLength[suffixLink[v]] for all states v
, wheremaxLength[v]
is the longest path from the root to thev
state andsuffixLink[v]
is a suffix link for this state.
Constructing a suffix automaton in a linear time is a well-known problem. The second part is just a simple traversal. Thus, the total time complexity is O(n)
.

kraskevich
- 18,368
- 4
- 33
- 45
-
Thank you very much, it's exactly that I need. For if it helps someone else leave the link where I found the algorithm. [english google's translate](https://translate.google.es/translate?hl=es&sl=ru&tl=en&u=http%3A%2F%2Fe-maxx.ru%2Falgo%2Fsuffix_automata) [original in russian](http://e-maxx.ru/algo/suffix_automata) – corxil Jan 08 '15 at 20:16