Squares (fragments of the form $xx$, for some string $x$) are arguably the most natural type of repetition in strings. The basic algorithmic question concerning squares is to check if a given string of length $n$ is square-free, that is, does not contain a fragment of such form. Main and Lorentz [J. Algorithms 1984] designed an $\mathcal{O}(n\log n)$ time algorithm for this problem, and proved a matching lower bound assuming the so-called general alphabet, meaning that the algorithm is only allowed to check if two characters are equal. However, their lower bound also assumes that there are $\Omega(n)$ distinct symbols in the string. As an open question, they asked if there is a faster algorithm if one restricts the size of the alphabet. Crochemore [Theor. Comput. Sci. 1986] designed a linear-time algorithm for constant-size alphabets, and combined with more recent results his approach in fact implies such an algorithm for linearly-sortable alphabets. Very recently, Ellert and Fischer [ICALP 2021] significantly relaxed this assumption by designing a linear-time algorithm for general ordered alphabets, that is, assuming a linear order on the characters that permits constant time order comparisons. However, the open question of Main and Lorentz from 1984 remained unresolved for general (unordered) alphabets. In this paper, we show that testing square-freeness of a length-$n$ string over general alphabet of size $\sigma$ can be done with $\mathcal{O}(n\log \sigma)$ comparisons, and cannot be done with $o(n\log \sigma)$ comparisons. We complement this result with an $\mathcal{O}(n\log \sigma)$ time algorithm in the Word RAM model. Finally, we extend the algorithm to reporting all the runs (maximal repetitions) in the same complexity.
翻译:平方(即形式为$xx$的片段,其中$x$为某个字符串)可以说是字符串中最自然的重复类型。关于平方的基本算法问题是检查给定长度为$n$的字符串是否无平方,即是否不包含这种形式的片段。Main和Lorentz [J. Algorithms 1984] 为该问题设计了$\mathcal{O}(n\log n)$时间复杂度的算法,并在假设所谓一般字母表(即算法仅允许检查两个字符是否相等)的情况下证明了匹配的下界。然而,他们的下界还假设字符串中存在$\Omega(n)$个不同符号。作为开放问题,他们问道:若限制字母表大小,是否存在更快的算法?Crochemore [Theor. Comput. Sci. 1986] 针对恒定大小字母表设计了线性时间算法,结合近期成果,其方法实际上暗示了在线性可排序字母表上存在此类算法。最近,Ellert和Fischer [ICALP 2021] 通过设计针对一般有序字母表(即假设字符间存在允许常数时间顺序比较的线性序)的线性时间算法,显著放宽了这一假设。然而,Main和Lorentz在1984年提出的关于一般(无序)字母表的开放问题仍未解决。在本文中,我们证明:对于大小为$\sigma$的一般字母表,测试长度为$n$的字符串是否无平方可通过$\mathcal{O}(n\log \sigma)$次比较完成,且无法通过$o(n\log \sigma)$次比较完成。我们用一个在Word RAM模型下运行时间为$\mathcal{O}(n\log \sigma)$的算法补充了这一结果。最后,我们将该算法扩展为以相同复杂度报告所有游程(最大重复)。
Alphabet is mostly a collection of companies. This newer Google is a bit slimmed down, with the companies that are pretty far afield of our main internet products contained in Alphabet instead.https://abc.xyz/