arXiv:2610.38114cs.DSOct 2026

Manuscript Offprint

arXiv:2610.38114

Output-Sensitive Enumeration of Maximal Closed Substrings in Linear Space

Johannes Fischer, Dominik Köppl

Abstract

We revisit the output-sensitive enumeration of maximal closed substrings (MCSs). Given a string of length nn over an integer alphabet, let kk 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 ε>0\varepsilon > 0, we can report all MCSs in O(nlog⁡n+klog⁡εn)O(n\sqrt{\log n} + k \log^\varepsilon n) time using O(n)O(n) words of space, or in O(nlog⁡n+klog⁡log⁡n)O(n\sqrt{\log n} + k \log \log n) time using O(nlog⁡log⁡n)O(n \log \log n) words. Both bounds include preprocessing and report occurrences by their endpoints. In particular, these algorithms improve the known O(nlog⁡n)O(n \log n) 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 O(nlog⁡n+k)O(n \log n + k)-time algorithm for constant-size alphabets; their subsequent journal version [2] achieves O(nlog⁡n)O(n \log n) worst-case time. Kosolobov [3] characterized MCSs equivalently as closed repeats and established that their worst-case number is Θ(nlog⁡n)\Theta(n \log n), already for binary strings. Jain and Mhaskar [4] developed compact representations of all closed substrings and additional O(nlog⁡n)O(n \log n)-time MCS algorithms. Two recent SPIRE 2026 papers address different computational settings: Shibata et al. [5] obtain an O(nlog⁡n)O(n \log n)-time, O(n)O(n)-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 kk of MCS occurrences can be much smaller than nn. For example, let T=ambmT = \mathbf{a}^m \mathbf{b}^m with m≥1m \geq 1 and n=2mn = 2m. Its only MCSs are the two full runs T[1..m]=amT[1..m] = \mathbf{a}^m and T[m+1..2m]=bmT[m+1..2m] = \mathbf{b}^m: shorter substrings within a run can be extended while remaining closed, whereas substrings crossing the run boundary have no nonempty border. We conclude that k=2k = 2, independent of nn. This contrasts with the worst-case Θ(nlog⁡n)\Theta(n \log n) output size and illustrates the benefit of reducing the input-dependent part of the running time: choosing 0<ε≤1/20 < \varepsilon \leq 1/2, our linear-space bound specializes to O(nlog⁡n)O(n\sqrt{\log n}) total time on this family, compared with the O(nlog⁡n)O(n \log n) 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 tnextt_{\text{next}} denoting the time for a succeeding-occurrence query nexti(h)\text{next}_i(h) with h≥2h \geq 2, the enumeration takes O(n+ktnext)O(n + kt_{\text{next}}) 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:

O(nlog⁡n+klog⁡εn) timeand O(n) words of space,O(n\sqrt{\log n} + k \log^\varepsilon n) \text{ time} \quad \text{and } O(n) \text{ words of space}, O(nlog⁡n+klog⁡log⁡n) timeand O(nlog⁡log⁡n) words of space,O(n\sqrt{\log n} + k \log \log n) \text{ time} \quad \text{and } O(n \log \log n) \text{ words of space},

for any fixed 0<ε<10 < \varepsilon < 1. Compared with the earlier O(nlog⁡n+k)O(n \log n + k) output-sensitive bound [1], our input-dependent term is smaller, at the price of a polylogarithmic factor per output. For 0<ε<10 < \varepsilon < 1, the first bound is o(nlog⁡n)o(n \log n) when k=o(nlog⁡1−εn)k = o(n \log^{1-\varepsilon} n), and the second is o(nlog⁡n)o(n \log n) when k=o(nlog⁡n/log⁡log⁡n)k = o(n \log n / \log \log n).

Interestingly, the O(nlog⁡n)O(n\sqrt{\log n})-term in the time bound is similar to best linear-space solution for computing the rightmost Lempel-Ziv parsing of a text of length nn and alphabet size ≤n\leq n [8]. This is no big surprise, as one can easily compute the rightmost LZ-parsing from all MCSs: if T[i..j]=αβαT[i..j] = \alpha\beta\alpha is an MCS with maximal border α\alpha, there is no occurrence of α\alpha in between, so the first occurrence T[i..i+∣α∣)T[i..i + |\alpha|) of α\alpha is the rightmost occurrence to the left of the second occurrence T(j−∣α∣..j]T(j - |\alpha|..j], allowing us to point to jj if there is an LZ-phrase starting at j−∣α∣+1j - |\alpha| + 1. 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 Ω(nlog⁡σ)\Omega(n \log \sigma) lower bound for closed repeats over a general ordered alphabet, even when the output has size O(n)O(n). It does not apply to our integer-alphabet word-RAM model.

Throughout the paper, T=aabaaabbaabaaT = \text{aabaaabbaabaa} serves as our running example, and Figure 1 illustrates its MCS occurrences.