Manuscript Offprint
arXiv:2610.38114Output-Sensitive Enumeration of Maximal Closed Substrings in Linear Space
Abstract
We revisit the output-sensitive enumeration of maximal closed substrings (MCSs). Given a string of length over an integer alphabet, let denote its number of MCS occurrences. We combine a right-to-left sweep on the string with range-successor queries to obtain all MCSs with two deterministic word-RAM bounds: for any fixed , we can report all MCSs in time using words of space, or in time using words. Both bounds include preprocessing and report occurrences by their endpoints. In particular, these algorithms improve the known worst-case time bound for sufficiently small output.
1 Introduction
A string is closed if it is a single character or has a nonempty border occurring only as its prefix and suffix. A maximal closed substring (MCS) is a closed occurrence that cannot be extended by one character to either side while preserving closedness. MCSs capture runs and certain maximal gapped repeats. Badkobeh et al. [1] introduced MCSs and gave an output-sensitive -time algorithm for constant-size alphabets; their subsequent journal version [2] achieves worst-case time. Kosolobov [3] characterized MCSs equivalently as closed repeats and established that their worst-case number is , already for binary strings. Jain and Mhaskar [4] developed compact representations of all closed substrings and additional -time MCS algorithms. Two recent SPIRE 2026 papers address different computational settings: Shibata et al. [5] obtain an -time, -space online algorithm using a link-cut suffix tree, whereas Umezaki et al. [6] enumerate a compact family representation of MCS occurrences directly from run-length encoded input. Our focus is instead on output-sensitive enumeration from an uncompressed, static text.
A reason for studying output-sensitive enumeration is that the number of MCS occurrences can be much smaller than . For example, let with and . Its only MCSs are the two full runs and : shorter substrings within a run can be extended while remaining closed, whereas substrings crossing the run boundary have no nonempty border. We conclude that , independent of . This contrasts with the worst-case output size and illustrates the benefit of reducing the input-dependent part of the running time: choosing , our linear-space bound specializes to total time on this family, compared with the worst-case guarantees of the previous algorithms.
We revisit output-sensitive MCS enumeration with a right-to-left sweep that maintains right-maximal closed-substring candidates in order of their lengths. The algorithm groups successive candidates witnessing the same MCS into a compact representation, which we call a manacle, and processes creations and deletions (in the following called births and deaths) of manacles rather than all candidates. With denoting the time for a succeeding-occurrence query with , the enumeration takes time after preprocessing. For an integer alphabet of polynomial size, we obtain the following two end-to-end deterministic bounds, reporting each MCS by its endpoints:
for any fixed . Compared with the earlier output-sensitive bound [1], our input-dependent term is smaller, at the price of a polylogarithmic factor per output. For , the first bound is when , and the second is when .
Interestingly, the -term in the time bound is similar to best linear-space solution for computing the rightmost Lempel-Ziv parsing of a text of length and alphabet size [8]. This is no big surprise, as one can easily compute the rightmost LZ-parsing from all MCSs: if is an MCS with maximal border , there is no occurrence of in between, so the first occurrence of is the rightmost occurrence to the left of the second occurrence , allowing us to point to if there is an LZ-phrase starting at . So computing all MCSs of a string can be seen as a generalization of rightmost Lempel-Ziv, in a similar way as the array of all longest previous factors generalizes regular Lempel-Ziv.
As a further side note, the distinction in computational models matters: Kosolobov [3] proves an lower bound for closed repeats over a general ordered alphabet, even when the output has size . It does not apply to our integer-alphabet word-RAM model.
Throughout the paper, serves as our running example, and Figure 1 illustrates its MCS occurrences.
Lemma 1. If a nonempty border of has exactly two occurrences in , then is the longest border of . In particular, the border witnessing closedness is unique.
Proof. Let be a border of with . As and are both prefixes of , is a prefix of . As both are suffixes of , is a suffix of . So occurs in at position , which is greater than 1 because and smaller than because . This is a third occurrence of , a contradiction.
Our computational task will be to detect closed substrings of a given string. In order to constrain the output meaningfully, we say that an occurrence with is a maximal closed substring (MCS) of if is closed while neither nor is closed. We write for the number of MCSs of throughout this paper.
We report an MCS occurrence by its pair of endpoints . By doing so, outputting an MCS alone takes time regardless of the length of the MCS. We are now ready to state the problem we solve in this paper.
MCS-COMPUTATION Input: text . Output: all MCSs of , each MCS as a pair of endpoints .
The substrings of length one can be detected trivially by the following fact:
Fact 1. A character is an MCS if and only if .
To solve MCS-COMPUTATION in general, we introduce the following notation. For , define the longest common right-extension of two text positions

Figure 1: MCSs of (sentinels omitted). The nonempty longest border of each MCS is shown in dark gray. The substring is closed, right-maximal, but not left-maximal since it can be extended to the left by a to , which is also closed with border aab.
and for , define the longest common left-extension of two text positions
For and , we denote the succeeding occurrence of by , formally defined as
We define if no such exists, and let be the length of the longest extension starting at that has a later occurrence in the text, where if , i.e., if is the last occurrence of character in . An occurrence of a prefix of is also an occurrence of every shorter prefix, so is non-decreasing on and for .
3 Structure of Maximal Closed Substrings
We first show a basic but important connection between the nxt-function and MCSs:
Lemma 2. Let . Then is closed if and only if there is an with and . In that case is unique and equals the length of the longest border of .
Proof. () Put and . Then is a border of , proper because . An occurrence of inside starting at with contradicts , and an occurrence starting at ends beyond . So occurs exactly twice in , and is closed.
() Let be a border of with exactly two occurrences and . Its occurrences are at and at and there is none in between, so . holds since is proper.
The uniqueness of follows easily from Lemma 1.
The next lemma shows how closed substrings behave when extended by one character to the right or left in :
Lemma 3. Let with be closed, let be its longest border, let , and let . Then
(i) is closed
(ii) is closed .
Proof. (i) If , then is a border of . Any occurrence of starting at with would contain an occurrence of at , contradicting , and an occurrence starting at ends beyond . Hence is closed.
Conversely, let be closed with longest border , and let . If , then is a prefix of and occurs at , at and as a suffix at . But these are three distinct occurrences, because . If , then is a border of of length , contradicting the maximality of . We thus have , which means . The second equivalence follows from and .
(ii) If , then is a border of , and an occurrence of it at with would give an occurrence of at , which is impossible. Conversely, let be closed with longest border of length . If , that border is a suffix of , so it also occurs at , which lies strictly inside , giving a third occurrence. If , deleting the first character of both occurrences of that border yields a border of of length , a contradiction. So , forcing .
This allows us to characterize all MCSs in the following, algorithmically approachable way:
Corollary 1. Let . Then is an MCS if and only if there is an such that, with , the following conditions hold:
• ,
• (accounting for right-maximality), and
• (accounting for left-maximality).
Our algorithm first finds right-maximal closed substrings and then tests whether they admit a closed left extension. We call these right-maximal substrings candidates, formally defined as:
Definition 1. For the set of candidates at j is
Since a candidate witnesses the closed substring , the labels and lengths of the tuples in have some structure due to the definition of closed substrings. In detail, the next lemma shows that for increasing right endpoints of candidates at , the lengths of borders must also be increasing:
Lemma 4. Assume , and let with . Then , and we have for all . We call the depth range and the length of the candidate .
Proof. Let be a value attained by on . By monotonicity, is an interval, say . For every the position is an occurrence of , so . In addition, , either because it is larger or because it is . In both cases is not an occurrence of , so . So the pairs over all attained values are exactly the candidates, and their intervals partition .
See Fig. 2 for an example of the resulting structure of candidates. By Corollary 1 and Lemma 4, the candidates at correspond exactly to the closed substrings starting at that cannot be extended to the right, and such a candidate is an MCS if it cannot be extended to the left, either.
We now show that half the length of the suffix bounds the number of candidates at j.
Lemma 5. For every we have .


Figure 2: Candidate set at text position of our running example. Left: Visualization of in text range with MCSs starting at drawn as horizontal bars. The longest border of each MCS is filled in dark gray inside its bar. Right: Visualization of the depth ranges in Lemma 4 as a staircase. The number of steps is the cardinality of . Each step spans the depth range of its candidate, while its height gives the text position of the second starting position of its longest border (the right dark gray bar).
Proof. If , the claim is immediate. Otherwise, let as in Lemma 4. The labels satisfy and are integers, so . The lengths satisfy , so . Since , we have , and therefore .
To see what happens to the candidate set when moving from j to j-1, we show:
Lemma 6. Let . Then , and for , with ,
In particular .
Proof. Let and . A position is an occurrence of if and only if and is an occurrence of . By minimality of there is no occurrence of in , which means that no occurrence of starts in . Finally, if , then is an occurrence of and therefore the leftmost one. If does not occur after , then does not occur after .
Lemma 7. Let with . If , then . If , then is an MCS.
Proof. In the first case by Lemma 6, and . The second case is Corollary 1.
Retrieving all candidates at position and checking whether each defines an MCS or can be extended to the left would be too time-consuming for an output-sensitive algorithm. Instead, we group candidates connected by successive left extensions into a manacle, representing them compactly. Formally, we make a definition grouping maximal sequences of candidates connected by repeated left extension into what we call manacles. See Fig. 3 for an illustration.
Definition 2. A maximal sequence is called a manacle. We call its gap and its right end.

Figure 3: A sketch of a manacle. , , , and are shown both as step labels and as positions. The invariant gap is visible as the horizontal distance from the current start position to the matching occurrence of the second border. In each row, the two dark arms have equal length and are separated by a gap larger than an arm.
At step , a manacle represents the candidate with label and length . Step is called the birth and step the death of . We say that the candidates are locked in . In particular, the manacle locking a candidate is uniquely determined by the candidate's label and length:
Lemma 8. Let and . Then the manacle locking has death , and the MCS obtained at its death is .
Proof. Let be the manacle locking . By Lemma 7, continues from step to step if and only if , where . Walking to the left, the compared characters are against , so the number of successful steps is exactly , after which either a mismatch occurs or position 0 is reached, where the sentinel \1\Sigma\lfloor \text{cs}(s-1, q-1) \rfloor \leq s-1x \geq 1e(M) = q + r - 1MxT[x..q+r-1]\square$
4 Algorithm
In a preprocessing phase we build:
-
a data structure supporting constant-time lce and lcs queries: for instance from the suffix array of and of the reverse of , each with its LCP array and a range-minimum structure
-
the array next with next, obtained by a single right-to-left scan. It answers nxt in constant time
-
a weighted level ancestor structure on the suffix tree of (equivalently, a search for the suffix-array interval of a prefix of a given suffix), which returns the suffix-array interval of from and
-
a range successor structure on the suffix array, which returns the smallest text position greater than a given threshold inside a given suffix-array interval.
The last two together evaluate nxt for . By we denote the time cost of one such evaluation. In addition, the algorithm maintains a doubly linked list of manacles ordered by length, and buckets of manacles indexed by death step, where stores in an (unordered) bucket the manacles that die at step i.
Candidate depths:

Figure 4: Setting for calling fill . Light-gray candidates already start before i. After the insertion, the new candidate for also starts before i, and the unresolved part is shown as a gap.
Algorithm 1 scans the text from right to left, from down to . The explicit test at Line 3 at the start of each iteration reports the MCSs of length one. All longer MCSs are handled through manacles. At the beginning of the iteration for , the list represents the candidates of in increasing order of length. A manacle in dies at this step and yields an MCS starting at . Every other manacle survives and, by Lemma 7, automatically represents the corresponding candidate of : although its label decreases by one and its length increases by one, the stored values and remain unchanged. Thus, the transition from to only has to construct the new candidates that are not represented by surviving manacles.
This is the role of fill(), a procedure for finding new manacles. Removing a maximal consecutive block of dying manacles leaves an interval of candidate lengths that may have to be filled at step . Starting from a manacle in the doubly linked list , we scan left and right to find the maximal consecutive block of dying manacles containing , denoted . Let be the surviving predecessor of the block, if it exists, so newly created manacles must be inserted directly behind . If exists then its new length is , and therefore the first potentially missing length is . If the block reaches the head of the list we instead take . The upper endpoint is . The additional call fill handles depth 1 when the first manacle survives and no deleted block covers it.
Inside fill , the variable h is the first depth after the candidate represented by P (or h = 1 if ). The query finds the leftmost succeeding occurrence of length h. If , fill() returns, since no longer prefix has a succeeding occurrence. Otherwise, is the length of the candidate whose depth range starts at h. Now:
-
If , this candidate is missing and is created. By Lemma 4, its depth range is , so the scan can jump straight to . The new manacle is inserted after and put into the bucket where it will die. We continue with being the newly created manacle.
-
If r > b, then by construction there is already a surviving manacle for this candidate and we can simply return.
We now prove that calling fill finds the missing manacles, and inserts them at the right place in .
Lemma 9. Consider a call fill and assume
(F1) either and , or is a manacle of whose candidate at step has length
(F2) no candidate of whose length lies in is already represented in
Algorithm 1: Enumeration of all MCSs of . gives the length of the candidate of the manacle g at step j; gives the entry preceding f in ; gives the entry following f in .
Data: : doubly linked list of manacles ordered by length; : buckets 1 ; for all j 2 for do // invariant: represents , ordered by length (Theorem 1) 3 if and then output (easy - singleton MCS) 4 foreach do output ; mark M as dying 5 if j = 1 then return 6 topDone false 7 foreach that is still in do 8 10 if then ; topDone true 11 else 12 remove from 13 fill 14 if not topDone then fill 15
Procedure fill : 17 18 while do 19 20 if then return 21 create a manacle M with and 22 insert M into directly behind P (at the head if ); 23 insert M into 24
(F3) , or the candidate of whose depth range contains is already represented in .
Then the call inserts into those candidates of whose length lies in in increasing order of length directly behind .
Proof. At the start of each loop iteration, either and , or represents a candidate of of length . Additionally, the candidates of with lengths in have already been inserted by this call. This holds initially by (F1), and after inserting a candidate of length it is preserved by setting to that candidate and .
Let . If , then , so no candidate has its length in and returning is correct. Otherwise put . By the loop invariant and Lemma 4, is the candidate immediately following (or the first candidate if ), and its depth range is . If , then , so by (F2) this candidate is not yet represented. The algorithm inserts the manacle of the candidate directly behind , and continues with . If , then the depth range contains . So , and by (F3) this same candidate is already represented in . We conclude that no candidate with length in is missing and returning is correct.
Note that every iteration of the while-loop of fill() (except possibly the last) inserts one candidate, which means that fill() evaluates nxt at most times, where z is the number of inserted candidates.
Theorem 1. For every , at the beginning of the iteration for the list contains exactly one manacle per candidate of , ordered by increasing length, and each such manacle satisfies and for its candidate . Moreover, every manacle of is stored in the bucket for its death step .
Proof via induction on decreasing . Base case: For we have , since no exists, and is empty.
Induction hypothesis: Assume the statement holds at step , and consider the transition performed in iteration to establish it at step .
Inductive step: By the induction hypothesis and Lemma 8, the manacles in are exactly the manacles of whose candidate is left-maximal, i.e., satisfies . We call these dying and all others surviving. For each such manacle , the algorithm outputs , which is an MCS by Lemma 7.
By Lemma 7, every surviving manacle represents a candidate of with label and length , so its stored pair needs no change and the relative order by length is preserved. Conversely, let . If , its length lies in the interval . If , let . Then by Lemma 6 either , which happens exactly when the candidate of whose depth range contains survives, or . In the first case must be the length of that surviving candidate, as if were strictly inside its depth range, then , a contradiction. So the candidates of split into the shifted surviving ones and those whose length lies in
Because the depth ranges partition (Lemma 4) and , the union of the intervals of the calls of fill() in iteration , intersected with , is exactly : a maximal block of consecutive dying manacles contributes for its surviving predecessor , or if there is none, and if the first manacle of survives then depth 1 is covered by the final call fill.
We verify the conditions of Lemma 9. For a call belonging to a maximal dying block , condition (F1) holds because, if exists, it survives and its length at step is ; if it does not exist, then and . Condition (F2) holds because the surviving predecessor has length and the surviving successor (if any) has length strictly larger than , meaning that all surviving candidates lie outside . Additionally, the intervals contributed by distinct maximal dying blocks are disjoint, so candidates inserted by earlier calls lie outside as well. Thus no candidate with length in is represented when the call is made.
For (F3), suppose first that . Then survives. Since is the first depth in the depth range of at step , we have . Applying Lemma 6 at depth gives . Hence, lies in the depth range of the candidate represented by at step , as required by (F3). If , then by Lemma 6, so (F3) again holds.
It remains to check the additional call . It is made only when no dying block reaches the head of the list. Thus either was empty, in which case and , or the first manacle survives. In the latter case no represented candidate at step has length 1, so (F2) holds. And if , Lemma 6 applied at depth 2 to the first surviving candidate shows that depth 2 lies in its depth range. Thus (F1)–(F3) hold also for this call.
By Lemma 9, all calls insert only the candidates of with length in , each once and at their correct position. Together with the surviving manacles, therefore represents , ordered by length.
Finally, note that every newly created manacle is inserted into the bucket of its death step, and that the dying manacles are removed from .
Theorem 2. Algorithm 1 outputs exactly the MCSs of T.
Proof. MCSs of length one are reported in Line 3 in Algorithm 1. For each , Theorem 1 gives exactly one manacle for every . By Lemma 8, this manacle dies at step if and only if . In this case the algorithm reports . By Corollary 1, these are the MCSs of length at least 2 starting at . Finally, by Lemma 2, each such MCS determines a unique , hence a unique candidate , and therefore is reported exactly once.
Theorem 3. After preprocessing, Algorithm 1 runs in time plus evaluations of with , which results in time . It uses words of working space (in addition to the range successor data structure).
Proof. By Theorem 2 every manacle is output as exactly one MCS at its death, so at most manacles are ever created. Each manacle is created, inserted, output and removed once, at constant cost each, and the scan of a maximal block of dying manacles is charged to its members. Apart from these charges, each of the iterations costs time.
To bound the successor queries, let be the number of created manacles and the number of fill() calls associated with dying blocks. Every such block is nonempty, and each manacle dies only once, so . By Lemma 9, these calls jointly evaluate at most times. The only remaining fill() calls have parameters . Each of them evaluates at most once, answered in time by next. So at most evaluations have and cost each, while all lce and lcs queries take constant time.
For the space bound, all preprocessing structures other than the range-successor structure occupy words, and the only other storage is the bucket array , again words, together with the manacles. The record of a manacle is created when is born and is released as soon as dies, since a dead manacle is never inspected again. By Theorem 1, at the beginning of iteration the manacles in use are exactly those of the manacles representing , and during iteration the records of the dying manacles are still needed for the scan of while the manacles of are already being created, so at most records are in use at any instant. This is at most by Lemma 5.
Corollary 2. For every constant , all MCS occurrences of a string over a polynomially bounded integer alphabet can be enumerated deterministically with either of the following bounds in the word-RAM model:
-
total time and words of space, or
-
total time and words of space.
Both time bounds include preprocessing, and each MCS is reported by its pair of endpoints.
Proof. The suffix-array interval of a text fragment can be located in time using the suffix-tree weighted-ancestor structure of Belazzougui et al. [7], which takes words and construction time. Thus, reduces to a range successor query on the suffix array, with threshold , in addition to work.
By Proposition 9.7(a,b) of Kociumaka et al. [10], based on the range-successor structures of Belazzougui and Puglisi [8] and Gao et al. [9], range successor queries can be supported with either words and query time, or words and query time. Both structures have construction time. The other preprocessing structures are constructed in time and space. Substituting these bounds into Theorem 3 proves the two claims.
5 Future Work
A natural direction is faster reverse-online enumeration, with characters arriving from right to left. The MCSs beginning at are reported once becomes known, allowing a one-character delay to test left maximality. For a constant alphabet and known final length , we aim for words and amortized total time .
Our idea is to represent the candidate staircase visualized in Fig. 2 within the suffix tree, similar to [5]. In the suffix tree of T[i..n]$$_2, label each vertex by , the smallest later suffix position below , or if none exists. Along the path to leaf , the label at string depth is . Thus a label-changing edge with parent string depth identifies the candidate , where ; see Lemma 4. By Corollary 1, this edge witnesses the MCS if and only if , with the left sentinel used at . Representing the information as the color of this edge, reporting amounts to color-filtered enumeration of label-changing edges on a root-to-leaf path.
When arrives, the newly eligible later position becomes the minimum label of every old vertex on the root-to-leaf- path; other old labels remain unchanged. After this root-path assignment, we insert the new suffix.
A possible implementation uses a recursive macro/micro-tree decomposition of the suffix tree. Microtrees of items would store packed path summaries, including endpoint labels and colors of label changes. Contracting microtrees recursively gives levels. With constant-time local packed operations, a path update or a report visits fragments, apart from fragments charged to its outputs. An oversized microtree could be rebuilt after splitting: charging rebuilding work to insertions would suffice for an amortized update cost. The unresolved part is the dynamic update of the recursively defined macro/micro-tree decomposition.
References
[1] G. Badkobeh, A. De Luca, G. Fici, and S. J. Puglisi. Maximal Closed Substrings. In Proc. SPIRE, LNCS 13617, pp. 16–23, 2022. doi:10.1007/978-3-031-20643-6_2.
[2] G. Badkobeh, A. De Luca, G. Fici, and S. J. Puglisi. Finding maximal closed substrings. Theoretical Computer Science, 1060:115628, 2026. doi:10.1016/j.tcs.2025.115628.
[3] D. Kosolobov. Closed Repeats. arXiv:2410.00209, 2024.
[4] S. K. Jain and N. Mhaskar. Efficient Algorithms to Compute Closed Substrings. Theory of Computing Systems, 70, article 48, 2026. doi:10.1007/s00224-026-10290-x.
[5] H. Shibata, H. Umezaki, T. Mieno, Y. Nakashima, and S. Inenaga. Online computation of maximal closed substrings. In Proc. SPIRE 2026, to appear; arXiv:2607.00612, 2026.
[6] H. Umezaki, H. Shibata, Y. Nakashima, and S. Inenaga. Compact enumeration of maximal closed substrings in run-length encoded strings. In Proc. SPIRE 2026, to appear; arXiv:2607.02838, 2026.
[7] D. Belazzougui, D. Kosolobov, S. J. Puglisi, and R. Raman. Weighted Ancestors in Suffix Trees Revisited. In Proc. CPM, LIPIcs 191, article 8, 2021. doi:10.4230/LIPIcs.CPM.2021.8.
[8] D. Belazzougui and S. J. Puglisi. Range predecessor and Lempel-Ziv parsing. In Proc. SODA, pp. 2053–2071, 2016. doi:10.1137/1.9781611974331.ch143.
[9] Y. Gao, M. He, and Y. Nekrich. Fast preprocessing for optimal orthogonal range reporting and range successor with applications to text indexing. In Proc. ESA, LIPIcs 173, article 54, 2020. doi:10.4230/LIPIcs.ESA.2020.54.
[10] T. Kociumaka, J. Radoszewski, W. Rytter, and T. Waleń. Internal Pattern Matching Queries in a Text and Applications. SIAM Journal on Computing, 53(5):1524–1577, 2024. doi:10.1137/23M1567618.
11