English

Optimal Square Detection Over General Alphabets

Data Structures and Algorithms 2023-03-14 v1

Abstract

Squares (fragments of the form xxxx, for some string xx) 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 nn is square-free, that is, does not contain a fragment of such form. Main and Lorentz [J. Algorithms 1984] designed an O(nlogn)\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 Ω(n)\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-nn string over general alphabet of size σ\sigma can be done with O(nlogσ)\mathcal{O}(n\log \sigma) comparisons, and cannot be done with o(nlogσ)o(n\log \sigma) comparisons. We complement this result with an O(nlogσ)\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.

Keywords

Cite

@article{arxiv.2303.07229,
  title  = {Optimal Square Detection Over General Alphabets},
  author = {Jonas Ellert and Paweł Gawrychowski and Garance Gourdel},
  journal= {arXiv preprint arXiv:2303.07229},
  year   = {2023}
}

Comments

extended version of a paper published in SODA 2023

R2 v1 2026-06-28T09:14:27.498Z