paper

Counting Distinct (Non-)Crossing Substrings in Optimal Time

arXiv:2506.22728

Abstract

Let be a string of length . The problem of counting factors crossing a position -- Problem 64 from the textbook ``125 Problems in Text Algorithms'' [Crochemore, Lecroq, and Rytter, 2021] -- asks to count the number (resp. ) of distinct substrings in that have occurrences containing (resp. not containing) a position in . The solutions provided in their textbook compute and in time for a single position in , and thus a direct application would require time for all positions in . Their solution is designed for constant-size alphabets. In this paper, we present new algorithms which compute in total time for general ordered alphabets, and in total time for linearly sortable alphabets,for all positions in . We further derive model-dependent optimal bounds by separating the algorithms into preprocessing and linear-time postprocessing: for the preprocessing is run reporting, and for it is preprocessing based on longest previous non-overlapping factors (LPnF) and longest next factors (LNF). In particular, all values can be computed in time over general unordered alphabets in which direct accesses to alphabet characters are restricted to equality tests, and in time in the word RAM model, where denotes the number of distinct characters occurring in . For , the equality-testing complexity over general unordered alphabets is . We also show that our upper bounds are optimal for all of the aforementioned alphabet assumptions and computation models.

Counting Distinct (Non-)Crossing Substrings in Optimal Time · wovepaper