Suffix automata · Hard
A text-index experiment measures how many different contiguous fragments a string contains. Several ranges can spell the same fragment and should count only once. The input is large enough that materializing every substring is not a viable implementation; this is an explicitly specialist string-indexing exercise.
Return the number of distinct non-empty contiguous substrings without constructing or storing all of those substrings.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ text: string }
Fields
- text is a case-sensitive printable ASCII string. A fragment is a consecutive sequence of characters, not a subsequence.
Output
Return value
- Return one integer count. Different positions with identical text contribute one distinct fragment.
- The empty fragment is excluded and empty input returns zero. Do not normalize spaces, case or punctuation.
Constraints
- 0 <= text.length <= 200000; character codes are 32 through 126.
- The answer is at most n*(n+1)/2 and fits a safe JSON integer. No external packages or network lookups are available.
Examples
Example 1
{
"text": "aba"
}Returns
5Example 2
{
"text": "aaaa"
}Returns
4Required complexity
O(n) expected time and O(n) space using bounded-alphabet suffix-state indexing