Manuscript Offprint
arXiv:2611.01874Efficient Lempel-Ziv Prediction with the Run-Length BWT
Abstract
Lempel-Ziv Plurality (LZP) is an algorithm of Kosoy that predicts the next symbol of a string by using its Lempel-Ziv factorization, and makes at most mistakes on every string of length with LZ77 phrases. We give efficient online implementations of LZP and of a new variant of it. Our first implementation maintains a run-length encoded Burrows-Wheeler transform (RLBWT) of the reversed history. It implements LZP, uses bits, and has worst-case update and prediction times and , respectively, where is the size of the alphabet. This is an exponential improvement over Kosoy's implementation, whose update time is . We then introduce the Lempel-Ziv Majority (LZM) predictor, which has the same mistake bound and update time, and predicts in time independently of . These two predictors accept new symbols at any time, without knowing the alphabet in advance. For an alphabet known in advance, a different data structure gives time per symbol, which is constant when and .
Keywords: online learning, sequence prediction, mistake bounds, Lempel-Ziv factorization, Burrows-Wheeler transform, dynamic data structures
1. Introduction
Online sequence prediction is a classical problem in machine learning and algorithms. A predictor reads a sequence one symbol at a time, each time guessing the next symbol, then reading the true symbol and updating its state. What one would like is a single algorithm that is at once computationally efficient, general-purpose (able to learn a wide range of patterns from many domains), and equipped with a formal mistake bound that guarantees a small number of prediction mistakes for the sequences we care about. Unfortunately, we do not know of any predictor that satisfies all three properties.
The theoretical gold standard for sequence prediction is Solomonoff induction (Hutter et al., 2024), which gives higher weight to sequences with small Kolmogorov complexity: those generated by short computer programs. This predictor satisfies the second and third requirements, with guaranteed mistake bounds on all computable data; however, Solomonoff induction is itself uncomputable, as is the notion of Kolmogorov complexity it is based on. In order to build efficient predictors, we should relax the notion of string complexity to simpler patterns that are easier to recognize efficiently.
A natural repetitiveness measure is the size of the LZ77 parse: for a string is defined to be the number of phrases in its LZ77 factorization. This measure captures highly repetitive strings, which can be built by reusing and copying previous terms. The broader Lempel-Ziv approach was in fact first proposed as a computationally tractable stand-in for Kolmogorov complexity (Lempel and Ziv, 1976), and it has considerable expressive power: many rich families of deterministic sequences studied in combinatorics on words (Allouche and Shallit, 2003), such as automatic sequences, Sturmian words and many morphic sequences, have LZ77 parse size only logarithmic in their length
(Kosoy, 2026a; Navarro, 2022), and are therefore well-predicted by any algorithm whose error scales with . Predicting strings with small well can thus serve as a computable toy analogue of the Solomonoff ideal.
Kosoy's Lempel-Ziv Plurality predictor (LZP) does exactly this (Kosoy, 2026a). At each step it takes the current LZ77 phrase, finds the earlier places in the history where that phrase has occurred, and predicts the symbol that most often followed them. LZP makes at most mistakes on any length- sequence, so a highly repetitive sequence is predicted with correspondingly few errors. However, the given implementation of LZP reconstructs an auxiliary compressed index on each symbol's update and prediction, giving time for each operation when the alphabet is constant (Kosoy, 2026a, Section D.2.3).
Our first contribution removes this cost. Rather than storing the LZ77 factorization explicitly, we maintain a run-length encoded Burrows-Wheeler transform (RLBWT) of the reversed history. In this representation, all occurrences of a given phrase form a single contiguous interval, and the symbols that follow them can be counted efficiently using rank queries. Extending the current phrase by one symbol is a single incremental update. This implements LZP, while reducing the time of a single update to .
Our second contribution changes the prediction rule itself to make prediction faster. LZP prediction calculates the exact plurality in a range by iterating over all distinct symbols, costing an extra factor of the alphabet size . We define a weaker prediction rule that is only guaranteed to predict a symbol if it has over half of the votes. The resulting Lempel-Ziv Majority predictor (LZM) inherits the same mistake bound as LZP. It calculates the vote majority with a single query, using Boyer-Moore cancellation. This allows prediction to run as fast as updates, and precalculating prediction makes prediction time .
Our third contribution gives a faster implementation for small, fixed alphabets. Our predictors only need a small interface of a dynamic run-length encoded string that the dynamic RLBWT is implemented over. Each RLBWT update only requires local changes to the runs. This allows us to use the dynamic partial sums structure of Bille et al. (2018). The resulting LZP implementation does updates and predictions in worst-case time. In particular, we achieve worst-case constant time when and .
None of these implementations is fastest for all parameters (Table 1). In Section 5 we ask for a single dynamic run-length encoded string with range-majority queries whose operations all take worst-case time, where is the number of runs and is the word size. Such a structure would make LZM run in constant time per symbol whenever , for any alphabet. We believe that such a structure exists, and leave a construction for future work.
Compressed string indexes have also been used for prediction: Ktistakis et al. (2019) use the FM-index and Cunial et al. (2019) use a run-length BWT, both for predicting the next symbol of a sequence. Both approaches build indexes on fixed training data and neither establishes mistake
Table 1: Lempel-Ziv predictors for , with as defined in Section 2. All make at most mistakes. Our rows are Corollary 21 (1)–(3): they are deterministic, with worst-case times and space including temporary storage. First row follows Kosoy (2026a, Sections D.2.1 and D.2.3), assuming and counting only persistent state.
| Predictor | Update | Prediction | Space (bits) | Alphabet |
|---|---|---|---|---|
| LZP (Kosoy) | fixed | |||
| RLBWT-LZP | dynamic | |||
| RLBWT-LZM | dynamic | |||
| RLBWT-LZP, alphabet-indexed | fixed |
bounds. Our predictors instead maintain a dynamic index of the growing history and satisfy finite mistake bounds in terms of the number of LZ77 phrases.
Policriti and Prezza (2016) and Ohno et al. (2018) use dynamic run-length BWTs to compute the LZ77 factorization in compressed space, Bannai et al. (2018) use them for online LZ77 parsing, and Nishimoto et al. (2022) construct the RLBWT online in total time. We follow the layout of Policriti and Prezza (2016) and Ohno et al. (2018), implemented with the dynamic sequences of Navarro and Nekrich (2014) and the dynamic partial sums of Bille et al. (2018). Our predictors use BWT rank queries to obtain continuation counts, and LZM uses an additional range-majority query, all with worst-case bounds per symbol. The bound of Kempa and Kociumaka (2020) on the number of BWT runs relates our costs to .
For LZM, we use a weighted form of the Boyer-Moore majority vote (Boyer and Moore, 1991). Dynamic range-majority queries have also been studied for general sequences (Elmasry et al., 2012), including entropy-compressed ones (Gagie et al., 2020); our structure works on run-length encoded sequences, and weights runs by their lengths.
2. Setting
We recall the prediction framework of Kosoy (2026a), fix our model of computation, and define the two prediction rules that we implement, which share the mistake bound of LZP. Proofs omitted from this section are in Appendix A.
2.1. Notation and Model of Computation
Strings are indexed from zero: for a string we write for its -th symbol, , and . The reverse of is . For strings and , is the number of occurrences of in , i.e. the number of positions with . For a condition is 1 if holds and 0 otherwise. Logarithms are to base 2, and inside -notation we let for and .
We use the word RAM with a word size that is fixed throughout an execution, and with the usual constant-time arithmetic (including multiplication), comparison, Boolean and shift operations. For an input of length , we assume that , chosen large enough that positions, counts and pointers fit in a word. The space of an algorithm is the largest number of words that it occupies at any time, including temporary storage. We state space bounds in words, and translate them to bits for . All algorithms in this paper are deterministic. All time bounds are worst-case rather than amortized.
The alphabet can be either fixed or dynamic. In the fixed settings, is known ahead of time and the time and space bounds allow dependency on . We set , and we assume so that each symbol fits in a single word. In the dynamic settings, all -bit words are valid symbols, . Here denotes the number of distinct symbols observed so far; for bounds on a complete input , it denotes the number of distinct symbols in .
2.2. The Prediction Protocol
Following Kosoy (2026a), a predictor is a tuple , where is the set of internal states, is the initial state, is the state-update function and is the state-prediction function. On an input , the predictor starts at and, at each round , predicts , observes , and updates its state to . The number of mistakes and the maximal state size are
Kosoy (2026a) calls a predictor statistically efficient with respect to a complexity measure if for all , and computationally efficient if each round takes time polynomial in the size of the state and . For every fixed alphabet, our predictors are statistically and computationally efficient with respect to .
The update time and prediction time of a predictor are the worst-case times of computing and , respectively. Any predictor can be cached: the update computes right after and stores it in the state, so that a prediction only reads the stored symbol. This adds the prediction time to the update time, makes the prediction time , and adds one word to the state; the predictions, and hence the mistakes, are unchanged.
2.3. Lempel-Ziv Factorization and LZP
Definition 1 (LZ77 factorization) The LZ77 factorization of a string is the unique decomposition into phrases, such that each is either a symbol that does not occur earlier in , or the longest prefix of that has an earlier occurrence in . The LZ77 complexity is the number of phrases.
A phrase may overlap its earlier occurrence: if starts at position , it may be copied from a source starting at any , even if . We denote for the input string . We let the last phrase of the empty string be the empty string. As is given online, its last phrase evolves as follows.
Lemma 2 (Phrase extension) Let have last phrase , and let . If , the factorization of is obtained from that of by extending its last phrase to . Otherwise, it is obtained by appending the new phrase .
For a history with last phrase , the votes for the next symbol are the continuations of the earlier occurrences of :
Definition 3 (LZP; Kosoy, 2026a, Alg. 7) On a history with last phrase , LZP predicts a symbol maximizing , breaking ties arbitrarily.
Definition 4 (Majority-respecting predictors) A predictor is majority-respecting if, on every history with last phrase , it predicts whenever .
LZP is majority-respecting, and so is the Lempel-Ziv Majority (LZM) predictor defined in Section 4.3 (Theorem 19).
Theorem 5 (Mistake bound) Every majority-respecting predictor makes at most mistakes on every with , where . In particular, this holds for LZP and LZM.
The argument of Kosoy (2026a, Appendix D) for LZP uses only the strict-majority property, so it also establishes the bound for every majority-respecting predictor; for completeness, we include the full argument in Appendix A.
2.4. The Burrows-Wheeler Transform
Let be the sentinel symbol and denote \Sigma' = \Sigma \cup \{\}$$. We order\Sigma<cc \in \Sigma(\Sigma')^*$.
Let be a sentinel-terminated string, where , and let .
For such a string , its suffix array is the permutation of starting positions ordered by lexicographic ordering of the relevant suffixes:
The Burrows-Wheeler transform (Burrows and Wheeler, 1994) is the string of symbols preceding the suffixes of in suffix-array order: has length and , with wraparound Y[-1] := Y[N-1] = \(Ferragina et al., 2007; Navarro, 2022)j \in [0, N)jY[\mathrm{SA}[j]:N]L[j].^1$
For and , let be the number of occurrences of in up to (but not including) index :
Let count the symbols of smaller than :
The set of all suffixes beginning with a given pattern occupies a suffix-array interval . For nonempty , the pattern has exactly occurrences in , and the interval is empty when no such occurrences exist. For the empty pattern, we set , containing all suffix-array rows.
BWT supports efficient pattern matching in , by backward search (Ferragina and Manzini, 2000, 2005), whose steps extend the pattern one symbol at a time: for ,
- The name comes from the Burrows-Wheeler matrix, whose rows are the cyclic rotations of in lexicographic order: row starts with the suffix and ends with . We do not use the matrix itself.
5
A run of $L$ is a maximal block of equal symbols, and $r(L)$ is the number of runs. The run-length encoded BWT (RLBWT) stores $L$ as the sequence of its runs. $r(L)$ is small for repetitive texts: it can be bounded in terms of the LZ77 complexity of the underlying text (see Lemma 20). ### 3. Dynamic Run-Length Encoded Strings A run-length encoding (RLE) represents a string $L = c_1^{e_1} c_2^{e_2} \cdots c_r^{e_r}$, where $c_j \neq c_{j+1}$ and $e_j \geq 1$, by its run heads $c_j$ and run lengths $e_j$, using space proportional to the number $r$ of runs rather than to the length of $L$. Our predictors will store their BWT this way (Section 4). Everything LZP needs from the representation is captured by the following interface, which we describe independently of its application. Proofs omitted from this section are in Appendix B. **Definition 6 (Incremental RLE)** An incremental RLE structure maintains a string $L \in \Sigma^*$ of length $N$, initially empty, supporting the following operations: * $\text{rank}_c(L, i) = |\{q < i \mid L[q] = c\}|$, for $c \in \Sigma$ and $0 \leq i \leq N$; * $C_L(c) = |\{q < N \mid L[q] < c\}|$, for $c \in \Sigma$; * $\text{insert}(i, c)$, for $0 \leq i \leq N$ and $c \in \Sigma$, which replaces $L$ by $L[:i] c L[i:]$. The queried symbols need not occur in $L$. We write $T_{\text{rank}}$, $T_{\text{count}}$ and $T_{\text{ins}}$ for the worst-case times of the three operations, and $S$ for the number of words used, including temporary storage. These are functions of the current number of runs $r$ (and of $w$ and $\sigma$), nondecreasing in $r$; when $L$ is empty, we evaluate them at $r = 1$. This interface is weaker than that of general dynamic RLE structures, which also support access, select and deletions; our results only rely on the operations above, so any structure implementing them suffices. LZM (Section 4.3) needs one additional query, which returns the majority symbol of a range of $L$ whenever one exists. **Definition 7 (Augmented incremental RLE)** An augmented incremental RLE structure is an incremental RLE structure that also supports the query candidate$(a, b) \in \Sigma \cup \{\perp\}$, for $0 \leq a \leq b \leq N$, in worst-case time $T_{\text{cand}}$, with the following one-sided guarantee: if some $c \in \Sigma$ occurs in $L[a : b]$ more than $(b - a)/2$ times, then candidate$(a, b) = c$; otherwise, the answer may be arbitrary. The following lemma describes how an insertion changes the runs of $L$. **Lemma 8 (Run edits)** An operation insert$(i, c)$ changes the runs of $L$ in one of three ways: it extends a run of $c$ by one symbol; it splits a run of another symbol $c'$ into two runs of $c'$, with a new run $c^1$ between them; or it inserts a new run $c^1$ between two runs of symbols other than $c$, or at either end of $L$. In particular, no run is ever deleted, and no two runs are merged. Our implementations are built from the following two primitives. **Definition 9 (Partial sums)** A partial-sums structure maintains a sequence $v_1, \ldots, v_s$ of nonnegative integers under updates and insertions of entries, supporting the queries $\text{sum}(k) := v_1 + \cdots + v_k$ and search$(t)$, the smallest $k$ with $\text{sum}(k) \geq t$. **Definition 10 (Dynamic sequence)** A dynamic sequence maintains a string $X \in \Sigma^*$ under the operations $\text{rank}_c(X, j)$ and $\text{insert}(j, c)$, defined as for $L$ in Definition 6. ## 3.1. Balanced-Tree Implementation We first implement an incremental RLE structure following the layout of Policriti and Prezza (2016) and Ohno et al. (2018), with components that have worst-case time bounds. We call the symbols that occur in $L$ active. The structure represents the string $L = c_1^{e_1} \cdots c_r^{e_r}$ by the following substructures: * $A$: a partial-sums structure over the run lengths $e_1, \ldots, e_r$; * $A_c$, for every active symbol $c$: a partial-sums structure over the lengths of the runs of $c$, in order; * $L' := c_1 c_2 \cdots c_r$: the string of run heads, stored as a dynamic sequence;$^2$ * $F$: a dictionary of the active symbols, ordered by $\Sigma$, with the number of occurrences of each symbol in $L$. If $i=0$ or $c$ is not active, which we test using $F$, then $\text{rank}_c(L, i) = 0$. Otherwise, to answer $\text{rank}_c(L, i)$, let $k := A.\text{search}(i) - 1$ be the number of runs before the run containing $L[i-1]$, whose symbol is $L'[k]$. Then $A.\text{sum}(k)$ symbols precede it, and $\text{rank}_c(L', k)$ of the runs before it are runs of $c$, so\text{rank}_c(L, i) = A_c.\sum \bigl(\text{rank}_c(L', k) \bigr) + \bigl(i - A.\sum(k) \bigr) \left[ L'[k] = c \right]. \tag{2}
Similarly, $C_L(c)$ is the sum of the occurrences stored in $F$ for the symbols smaller than $c$. **Lemma 11 (Balanced trees)** There is an incremental RLE structure with $T_{\text{rank}}, T_{\text{count}}, T_{\text{ins}} = O(\log r)$ and $S = O(r)$ words, which supports symbols that are not known in advance. The partial-sums structures and $F$ are balanced search trees with subtree sums. For $L'$, which needs rank queries over a growing alphabet, we use the worst-case dynamic sequences of Navarro and Nekrich (2014, Theorem 10.5). ## 3.2. Alphabet-Indexed Implementation We now give a second incremental RLE structure, for an alphabet $\Sigma = \{0, \ldots, \sigma-1\}$ known in advance. It is faster than the structure of Lemma 11 when $\sigma$ is small, e.g. $\sigma = O(1)$, but its insertion time and its space grow linearly with $\sigma$. By Lemma 8, an insertion changes the sequence of run lengths only by adding one to an entry, inserting an entry equal to one, or dividing an entry into two entries with the same total. Such changes can be implemented faster than with balanced trees, using the following result of Bille et al. **Lemma 12 (Partial sums; adapted from Bille et al., 2018)** There is a partial-sums structure that maintains a sequence of $s$ nonnegative $w$-bit integers in $O(s+1)$ words, and supports sum and search queries, adding one to an entry, inserting an entry equal to 0 or 1, and dividing an entry into two consecutive entries with the same total, each in $O(1 + \log s / \log w)$ worst-case time. 2. As a string, $L'$ is indexed from zero, so $L'[j-1] = c_j$, while runs and the entries of partial-sums structures are numbered from one. The partial-sums structures of this lemma do not store the run heads, so we use one of them per symbol. We keep: * $A$: a partial-sums structure of Lemma 12 over the run lengths $e_1, \ldots, e_r$; * $W_c$, for every $c \in \Sigma$: a partial-sums structure of the same kind and length as $A$, whose $j$-th entry is $e_j$ if $c_j = c$, and 0 otherwise; * cnt: an array whose entry cnt[$c$] is $C_L(c)$, for every $c \in \Sigma$. We have $\operatorname{rank}_c(L,0) = 0$. For $i > 0$, let $k := A \cdot \operatorname{search}(i) - 1$ as before. Then, for every $c \in \Sigma$, including symbols that do not occur in $L$,\operatorname{rank}_c(L,i) = W_c \cdot \operatorname{sum}(k) + \left(i - A \cdot \operatorname{sum}(k)\right) \left[W_c \cdot \operatorname{sum}(k+1) > W_c \cdot \operatorname{sum}(k)\right].
Hence a rank query uses four operations of Lemma 12, on $A$ and $W_c$ only, and takes $O(1 + \log r / \log w)$ time, independently of $\sigma$. An insertion finds the symbols of the runs next to position $i$ by probing all the structures $W_c$, and repeats each of its at most two run edits in all of them, with zero entries in the structures of the other symbols. A query $C_L(c)$ reads one entry of cnt, and inserting $c$ adds one to the entries cnt[$y$] with $y > c$, in $O(\sigma)$ time. Lemma 13 (Alphabet-indexed structure) For an alphabet $\Sigma = \{0, \ldots, \sigma-1\}$ that is known in advance, there is an incremental RLE structure with $T_{\operatorname{rank}} = O(1 + \log r / \log w)$, $T_{\operatorname{count}} = O(1)$, $T_{\operatorname{ins}} = O(\sigma(1 + \log r / \log w))$, and $S = O(\sigma(r+1))$ words. ### 3.3. Range Majority with Cancellation Summaries To answer candidate queries, we augment the balanced-tree implementation of Section 3.1 with Boyer-Moore summaries (Boyer and Moore, 1991). Boyer and Moore explain their majority algorithm as a floor fight: each voter knocks down one voter of a different candidate and is knocked down in turn. When no further pairs remain, all voters left standing support the same candidate, and a candidate with a strict majority always has voters left. Their algorithm runs this fight left to right, keeping only the surviving candidate and the number of survivors. We apply this to the tree $A$, which has one node per run. A run $c^e$ has no inner cancellation and contributes $e$ votes for $c$ at once. Fights on disjoint parts of $L$ can be held separately and their outcomes merged: if $p$ remaining votes for $c$ meet $q$ remaining votes for $c' \neq c$, another $\min\{p, q\}$ pairs cancel out. So each node stores the outcome of the fight within its subtree, and a query merges $O(\log r)$ such outcomes. Formally, a summary is either $\perp$ or a pair $(c, p)$ with $c \in \Sigma$ and $p \geq 1$. It is valid for a multiset $M$ of symbols if $M$ can be partitioned into pairs of distinct symbols together with $p$ copies of $c$ (or with nothing, for $\perp$). The summary of a (possibly partial) run $c^e$ is $(c, e)$. Two summaries are combined by\left(c, p\right) \star \left(c', q\right):=\begin{cases}\left(c, p+q\right)&\text{if } c = c',\\left(c, p-q\right)&\text{if } c \neq c' \text{ and } p > q,\\left(c', q-p\right)&\text{if } c \neq c' \text{ and } q > p,\\perp&\text{if } c \neq c' \text{ and } p = q,\end{cases} \tag{3}
and $\perp$ is neutral: $\perp \star s = s \star \perp = s$. Lemma 14 (Cancellation summaries) If $s_1$ and $s_2$ are valid summaries for $M_1$ and $M_2$, then $s_1 \star s_2$ is a valid summary for $M_1 \uplus M_2$. If a symbol $c$ occurs in $M$ more than $|M|/2$ times, then every valid summary of $M$ is $(c,p)$ for some $p \geq 1$. Proof If the two candidates are equal, their uncancelled copies add up. Otherwise, we pair $\min\{p,q\}$ copies of the first candidate with copies of the second, and the rest of the larger side remains uncancelled. For the second claim, let $c$ occur $m_c > |M|/2$ times. A pair of distinct symbols contains at most one copy of $c$ and at least one of the $|M|-m_c$ other symbols, so at least $m_c - (|M|-m_c) > 0$ copies of $c$ are left uncancelled. Since all uncancelled symbols are equal, the summary is $(c,p)$ with $p \geq 1$. The operation $\star$ is not associative: for the run sequence $a^3d^2b^3d^2c^3$, the final summary when combining left-to-right is $(c,3)$, while combining right-to-left gives $(a,3)$. The same example shows that the summary does not always compute the plurality, as $d$ is the unique most common symbol but no parenthesization leads to $d$ as the winning summary. Each node of the tree stores the summary of its subtree, obtained by combining the summaries of its left subtree, its own run and its right subtree. Like a subtree sum, it depends only on the node and its children, so an insertion recomputes it only at the nodes on the path from the changed nodes to the root and at the nodes changed by rebalancing. A query candidate $(a,b)$ is then computed like a range-sum query in a tree with subtree sums, with $\star$ in place of $+$ (Algorithm B.1 in Appendix B): it combines the summaries of the two partial runs at the ends of $L[a:b]$ and of $O(\log r)$ whole runs and subtrees between them, and returns the candidate symbol of the result, or $\perp$ for the empty summary. By Lemma 14, this satisfies the guarantee of Definition 7. Lemma 15 (Majority tree) The structure of Lemma 11 can be extended to an augmented incremental RLE structure with $T_{\text{cand}}, T_{\text{rank}}, T_{\text{count}}, T_{\text{ins}} = O(\log r)$ using $S = O(r)$ words. ## 4. Lempel-Ziv Prediction with a Dynamic RLBWT We now implement LZP and LZM on top of the interfaces of Section 3. Both predictors maintain the BWT of the reversed history, together with the suffix-array interval of the reversed current phrase. Proofs omitted from this section are in Appendix C. ### 4.1. Maintaining the RLBWT of the Reversed History At round $t$, let $H := x[:t]$ be the history, $Y := H^{R}$, and $L := \text{BWT}(Y)$, of length $N = t + 1$. When a symbol $c$ is read, $Y$ becomes $cY = (Hc)^{R}$$. Reversing the history turns appending into prepending, which is the direction in which the BWT can be updated online (Policriti and Prezza, 2016; Ohno et al., 2018). Prepending $c$ changes $L$ in two places: in the row of $Y$, the symbol $#$ becomes $c$, and a new row for $cY$, with symbol $,$, is inserted. Since our interface supports neither deletions nor replacements, we keep the sentinel outside of the structure. Let $p_{\}$ be the position of $#$ 9 $$ in $L$, and let $L^-$ be the string over $\Sigma$ obtained from $L$ by deleting that position. The predictors store $L^-$ in an incremental RLE structure, together with the number $p_{\S}$. **Lemma 16 (Sentinel-free maintenance)** Let $L$, $L^-$ and $p_{\S}$ be as above. 1. For $c \in \Sigma$ and $0 \leq i \leq N$, $\operatorname{rank}_c(L, i) = \operatorname{rank}_c(L^-, i - [i > p_{\S}])$ and $C_L(c) = C_{L^-}(c) + 1$. 2. Replacing $Y$ by $cY$, for $c \in \Sigma$, amounts to the operation insert$(p_{\S}, c)$ on $L^-$, followed by the assignment $p_{\S} \leftarrow s$ fors := C_{L^-}(c) + \operatorname{rank}c(L^-, p{\S}) + 1, \tag{4}
computed before the insertion; $s$ is the row of the new suffix $cY$. Hence, rank and $C_L$ queries on $L$ take $O(T_{\operatorname{rank}})$ and $O(T_{\operatorname{count}})$ time, and prepending a symbol takes $O(T_{\operatorname{rank}} + T_{\operatorname{count}} + T_{\operatorname{ins}})$ time. The costs of our structures depend on the number of runs of the maintained BWT. For $x \in \Sigma^n$, letr_{\max}(x) := \max_{0 \leq t \leq n} r\big(\operatorname{BWT}(x[:t]^{R_{\S}})\big).
Deleting the sentinel does not increase the number of runs, so $L^-$ has at most $r_{\max}(x)$ runs throughout the execution. We bound $r_{\max}(x)$ in Lemma 20. ## 4.2. RLBWT-LZP Algorithm 1 gives RLBWT-LZP. The last step of the update follows Lemma 2: $J$ is nonempty if and only if $Pc$ occurs in $H$, and then the phrase becomes $Pc$. To see that the new interval is $[a', b' + 1)$ in line 6, note that prepending $c$ to $Y$ inserts a single row, that of the new suffix $cY$, and keeps the relative order of the old suffixes. Since $Y$ starts with $P^R$, the new suffix $cY$ starts with $cP^R$, so its row $s$ satisfies $a' \leq s \leq b'$. Hence the rows before $a'$ do not move, and the suffixes starting with $cP^R$ are the $b' - a'$ old ones and $cY$. Each row of $I$ corresponds to an occurrence of $P^R$ in $Y$, and hence of $P$ in $H$. Its BWT symbol precedes the occurrence in $Y$, so it is the symbol following the occurrence in $H$. The exception is the suffix $P$ of $H$, whose row is $p_{\S}$, with symbol $\$. Counting the rows of $I$ by their symbols therefore gives the votes of LZP: **Lemma 17 (Votes)** Let $H$ have last phrase $P$, let $I = [a, b) := I_Y(P^R)$, and for $c \in \Sigma$ let $\nu_c := \operatorname{rank}_c(L, b) - \operatorname{rank}_c(L, a)$. Then $\nu_c = v_H(P, c)$ for every $c \in \Sigma$. Moreover, $a \leq p_{\S} < b$ and $\sum_{c \in \Sigma} \nu_c = b - a - 1$. Since only active symbols can have positive votes, an active symbol with the largest $\nu_c$ maximizes $v_H(P, c)$ over all of $\Sigma$, so the prediction is that of LZP. (If all votes are zero, every symbol is tied.) Unlike online LZ77 parsing with an RLBWT (Bannai et al., 2018), the predictor never needs to know where the occurrences of $P$ are, and hence needs no suffix-array samples. **Theorem 18 (RLBWT-LZP)** Instantiate RLBWT-LZP with an incremental RLE structure of Definition 6. On every $x \in \Sigma^n$, it implements LZP, and hence makes at most $z(\log_2 n + 1)$ mistakes. Its update time is $O(T_{\operatorname{rank}} + T_{\operatorname{count}} + T_{\operatorname{ins}})$ and its prediction time is $O(\sigma T_{\operatorname{rank}})$; with caching, the update time is $O(\sigma T_{\operatorname{rank}} + T_{\operatorname{count}} + T_{\operatorname{ins}})$ and the prediction time is $O(1)$. Its space is $S + O(\sigma)$ words. The costs are evaluated at the largest number of runs $r_{\max}(x)$. Algorithm 1: RLBWT-LZP State: the incremental RLE structure over $L^{-}$ and $p_{\S}$; the endpoints a, b of the suffix-array interval $I = [a, b) := I_{Y}(P^{R})$, where P is the last phrase of H; a linked list of the active symbols. Initially: $L^{-}$ is empty, $p_{\S} = 0$, $I = [0, 1)$, and the list of active symbols is empty. (This represents the empty history: $Y = \$, L = \$ and $I = I_{Y}(\varepsilon)$.) UPDATE(c), when the symbol c is read 2 $\left| \begin{array}{c} J = [a', b'] \leftarrow I_{Y}(cP^{R}) $, by the backward step (1) from I \\ if rank_{c}(L, N) = 0 then append c to the list of active symbols \\ update L^{-} and p_{\S} to prepend c to Y, as in Lemma 16 \\ if J is nonempty then \\ | I \leftarrow [a', b' + 1) \\ else \\ | I \leftarrow I_{cY}(c), by a backward step from the full interval [0, N + 1) \\ end \end{array}\right.$ 10 PREDICT 11 if the list of active symbols is empty then 12 | predict an arbitrary symbol of $\Sigma$ 13 else 14 $\left| \begin{array}{c} \nu_{c} \leftarrow rank_{c}(L, b) - rank_{c}(L, a) \text{ for every active symbol } c \\ \text{predict an active symbol } c \text{ with the largest } \nu_{c} \end{array} \right.$ 15 16 end #### 4.3. RLBWT-LZM Algorithm 2: RLBWT-LZM State: the augmented incremental RLE structure over $L^{-}$ and $p_{\S}$; the endpoints of the interval $I = [a, b)$, as in Algorithm 1. Initially: as in Algorithm 1. 1 UPDATE(c), when the symbol c is read 2 | as in Algorithm 1, except line 3 3 PREDICT 4 $\left| \begin{array}{c} \hat{c} \leftarrow \text{candidate}(a, b-1) \text{ on } L^{-} \\ \text{if } \hat{c} \neq \bot \text{ then} \\ | \text{predict } \hat{c} \\ \text{else} \\ | \text{predict an arbitrary symbol of } \Sigma \end{array} \right.$ 9 end RLBWT-LZM (Algorithm 2) maintains the same $L^{-}$, $p_{\S}$ and I as RLBWT-LZP, but stores $L^{-}$ in an augmented incremental RLE structure (Definition 7), and it needs no list of active symbols. By Lemma 17, the votes are the BWT symbols of the rows of $I=[a,b)$ other than the sentinel row $p_{\S}$. In $L^{-}$, where the sentinel row is deleted, they occupy the range $[a,b-1)$. If some symbol has a strict majority of the votes, then it has a strict majority in $L^{-}[a:b-1]$, and the candidate query returns it. The sentinel has to stay out of the query: for a history $H=cc$, the last phrase is $c$ with the single vote $c$, and the rows of $I$ have the BWT symbols $c$, so a summary that treated $ as a symbol would cancel the only vote. Theorem 19 (RLBWT-LZM) Instantiate RLBWT-LZM with an augmented incremental RLE structure of Definition 7. It is majority-respecting, and hence makes at most $z(\log_2 n+1)$ mistakes on every $x\in\Sigma^n$. Its update time is $O(T_{\text{rank}}+T_{\text{count}}+T_{\text{ins}})$ and its prediction time is $O(T_{\text{cand}})$; with caching, the update time is $O(T_{\text{rank}}+T_{\text{count}}+T_{\text{ins}}+T_{\text{cand}})$ and the prediction time is $O(1)$. Its space is $S+O(1)$ words. The costs are evaluated at $r_{\max}(x)$. #### 4.4. Main Results Theorem 18 and Theorem 19 bound the costs in terms of $r_{\max}(x)$. To state our results in terms of $z$, we use the following bound. Lemma 20 (Runs; based on Kempa and Kociumaka, 2020) For every $x \in \Sigma^n$ with $n \geq 1$, $r_{\max}(x) = O(z \log z \log n)$, where $z = z(x)$. Combining the black-box theorems with the implementations of Section 3 and Lemma 20 gives our main results. Corollary 21 (Main results) Let $x \in \Sigma^n$ with $n \geq 2$, let $z = z(x)$, and let $w = \Theta(\log n)$. All of the following predictors make at most $z(\log_2 n + 1)$ mistakes on $x$. 1. RLBWT-LZP with the balanced-tree structure of Lemma 11 implements LZP. Its update time is $O(\log z + \log \log n)$, its prediction time is $O(\sigma(\log z + \log \log n))$, and its space is $O(z \log z \log n)$ words, i.e. $O(z \log z \log^2 n)$ bits. 2. RLBWT-LZM with the majority tree of Lemma 15 and cached predictions has update time $O(\log z + \log \log n)$, prediction time $O(1)$, and space $O(z \log z \log n)$ words. 3. If $\Sigma = \{0, \ldots, \sigma - 1\}$ is known in advance, RLBWT-LZP with the alphabet-indexed structure of Lemma 13 and cached predictions has update time $O(\sigma(1 + \log z / \log \log n))$, prediction time $O(1)$, and space $O(\sigma z \log z \log n)$ words. In particular, if $z = \text{polylog}(n)$, RLBWT-LZM uses polylog$(n)$ space and $O(\log \log n)$ time per round; if moreover $\sigma = O(1)$, the predictor in (3) uses polylog$(n)$ space and $O(1)$ time per round. Proof By Lemma 20, $r \leq r_{\max}(x) = O(z \log z \log n)$ throughout, so $\log r = O(\log z + \log \log n)$ and, since $\log w = \Theta(\log \log n)$, $\log r / \log w = O(1 + \log z / \log \log n)$. Every active symbol heads a run of $L^{-}$, so the list of active symbols fits in the space bounds. (1) follows from Theorem 18 and Lemma 11, and (2) from Theorem 19 and Lemma 15, where caching is free since $T_{\text{cand}} = O(\log r)$. For (3), Theorem 18 with Lemma 13 gives the update time $O(\sigma T_{\text{rank}} + T_{\text{count}} + T_{\text{ins}}) = O(\sigma(1 + \log r / \log w))$, and the space $O(\sigma(r_{\max}(x) + 1)) = O(\sigma z \log z \log n)$ words. $\blacksquare$ ## 5. Discussion and Future Work Our RLBWT construction reduces the cost of our predictors to that of an incremental RLE structure. The implementations of Section 3 are incomparable (Table 1): the balanced-tree structures take $O(\log r)$ time per operation, while insertion into the alphabet-indexed structure takes $O(\sigma(1 + \log r/\log w))$ time. Thus, for $r \geq w$, the alphabet-indexed structure is faster when $\sigma = o(\log w)$, and slower when $\sigma = \omega(\log w)$. By Theorem 19, a single structure with the following guarantees would give an LZM predictor that is at least as fast as every predictor in Table 1, in both update and prediction time. **Open Problem** Is there a deterministic augmented incremental RLE structure (Definition 7), for an alphabet that is not known in advance, that supports rank, $C_L$, insert and candidate in $O(1 + \log r/\log w)$ worst-case time each, using $O(r)$ words? By Theorem 19 and Lemma 20, such a structure would give cached LZM with worst-case update time $O(1 + \log z/\log \log n)$ and prediction time $O(1)$ for $w = \Theta(\log n)$, with the same mistake and space bounds as in Corollary 21. In particular, it would run in constant time per round whenever $z = \text{polylog}(n)$, with no assumption on the alphabet. The run lengths can already be maintained in $O(1 + \log r/\log w)$ time per operation (Lemma 12), which is optimal for partial sums with small updates (Pătrașcu and Demaine, 2005); the difficulty lies in the rank queries on the run heads and in the candidate queries. Munro and Nekrich (2015) support rank queries and insertions on a sequence of length $r$ over an alphabet fixed in advance in $O(\log r/\log \log r)$ worst-case time. This is slower than required when $w$ is large compared with $r$: if $w = \Theta(\log n)$ and $z = \text{polylog}(n)$, then $r = \text{polylog}(n)$ by Lemma 20, and their bound does not give constant time per round. We believe that the answer is positive nevertheless. Storing the run heads in a B-tree of fan-out $w^{\Theta(1)}$, as in the dynamic integer sets of Pătrașcu and Thorup (2014) and the partial sums of Bille et al. (2018), gives height $O(1 + \log r/\log w)$, so it would suffice to do $O(1)$ work per level. The main obstacles are keeping the per-node information for these two queries within linear space, and maintaining it with worst-case rather than amortized bounds. We leave a construction for future work. Finally, RLBWT-LZM uses $O(z \log z \log^2 n)$ bits of space, while Kosoy's implementation of LZP keeps only $O(z \log n)$ bits of persistent state (Table 1); it would be interesting to close this gap without slowing down the predictor. ## Acknowledgments I would like to thank Vanessa Kosoy for mentoring me throughout the project and for providing much helpful guidance. This work was carried out during the Principles of Intelligent Behavior in Biological and Social Systems (PIBBSS) 2026 Summer Fellowship, and I thank PIBBSS for continued support of the research work. It was also supported by the Advanced Research + Invention Agency (ARIA) of the United Kingdom and by Coefficient Giving in San Francisco, California. ## AI Methodology This work was developed through an extensive collaboration with AI systems. The writing was drafted with substantial assistance from AI models (OpenAI ChatGPT and Anthropic Claude), based on my research notes and guidance, and I then further reviewed and corrected it. The AI models also helped with proofs and literature review, and identified some errors in earlier drafts. I take full responsibility for the content of the paper. ## References Allouche, J.-P. and J. Shallit (2003). Automatic Sequences: Theory, Applications, Generalizations. 1st ed. Cambridge University Press. DOI: 10.1017/CBO9780511546563. Bannai, H., T. Gagie, and T. I (2018). "Online LZ77 Parsing and Matching Statistics with RLBWTs". In: 29th Annual Symposium on Combinatorial Pattern Matching (CPM 2018). Ed. by G. Navarro, D. Sankoff, and B. Zhu. Vol. 105. Leibniz International Proceedings in Informatics (LIPIcs). Dagstuhl, Germany: Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 7:1–7:12. DOI: 10.4230/LIPIcs.CPM.2018.7. Bille, P., A. R. Christiansen, P. H. Cording, I. L. Gørtz, F. R. Skjoldjensen, H. W. Vildhøj, and S. Vind (2018). "Dynamic Relative Compression, Dynamic Partial Sums, and Substring Concatenation". In: Algorithmica 80.11, pp. 3207–3224. DOI: 10.1007/s00453-017-0380-7. Boyer, R. S. and J. S. Moore (1991). "MJRTY—A Fast Majority Vote Algorithm". In: Automated Reasoning. Ed. by R. S. Boyer. Red. by W. Pase. Vol. 1. Dordrecht: Springer Netherlands, pp. 105–117. DOI: 10.1007/978-94-011-3488-0_5. Burrows, M. and D. J. Wheeler (1994). A Block-Sorting Lossless Data Compression Algorithm. Digital Equipment Corporation, Systems Research Center, 124. Cunial, F., J. Alanko, and D. Belazzougui (2019). "A Framework for Space-Efficient Variable-Order Markov Models". In: Bioinformatics 35.22, pp. 4607–4616. DOI: 10.1093/bioinformatics/btz268. Elmasry, A., M. He, J. I. Munro, and P. K. Nicholson (2012). Dynamic Range Majority Data Structures. DOI: 10.48550/arXiv.1104.5517.arXiv:1104.5517 [cs.DS]. URL: http://arxiv.org/abs/1104.5517. Feder, M., N. Merhav, and M. Gutman (1992). "Universal Prediction of Individual Sequences". In: IEEE Transactions on Information Theory 38.4, pp. 1258–1270. DOI: 10.1109/18.144706. Ferragina, P. and G. Manzini (2000). "Opportunistic Data Structures with Applications". In: Proceedings 41st Annual Symposium on Foundations of Computer Science, pp. 390–398. DOI: 10.1109/SFCS.2000.892127. — (2005). "Indexing Compressed Text". In: Journal of the ACM 52.4, pp. 552–581. DOI: 10.1145/1082036.1082039. Ferragina, P., G. Manzini, V. Mäkinen, and G. Navarro (2007). "Compressed Representations of Sequences and Full-Text Indexes". In: ACM Transactions on Algorithms 3.2, p. 20. DOI: 10.1145/1240233.1240243. Gagie, T., M. He, and G. Navarro (2020). "Compressed Dynamic Range Majority and Minority Data Structures". In: Algorithmica 82.7, pp. 2063–2086. DOI: 10.1007/s00453-020-00687-6. Hutter, M., D. Quarel, and E. Catt (2024). An Introduction to Universal Artificial Intelligence. Chapman and Hall/CRC. DOI: 10.1201/9781003460299. Kempa, D. and T. Kociumaka (2020). "Resolution of the Burrows-Wheeler Transform Conjecture". In: 2020 IEEE 61st Annual Symposium on Foundations of Computer Science (FOCS). Durham, NC, USA: IEEE, pp. 1002–1013. DOI: 10.1109/FOCS46700.2020.00097. Kosoy, V. (2026a). Stringological Sequence Prediction I: Efficient Algorithms for Predicting Highly Repetitive Sequences. DOI: 10.48550/ARXIV.2603.26852. URL: https://arxiv.org/abs/2603.26852v2. — (2026b). Stringological Sequence Prediction II: Right-to-Left Automaticity and Related Complexity Measures. DOI: 10.48550/ARXIV.2607.17369. URL: https://arxiv.org/abs/2607.17369. — (2026c). Stringological Sequence Prediction III: Layered Ziplines and a Tradeoff between Efficiency and Expressivity. DOI: 10.48550/arXiv.2609.19940. arXiv: 2609.19940. Ktistakis, R., P. Fournier-Viger, S. J. Puglisi, and R. Raman (2019). “Succinct BWT-Based Sequence Prediction”. In: Database and Expert Systems Applications. Ed. by S. Hartmann, J. Küng, S. Chakravarthy, G. Anderst-Kotsis, A. M. Tjoa, and I. Khalil. Vol. 11707. Lecture Notes in Computer Science. Cham: Springer International Publishing, pp. 91–101. DOI: 10.1007/978-3-030-27618-8_7. Lempel, A. and J. Ziv (1976). “On the Complexity of Finite Sequences”. In: IEEE Transactions on Information Theory 22.1, pp. 75–81. DOI: 10.1109/TIT.1976.1055501. Munro, J. I. and Y. Nekrich (2015). “Compressed Data Structures for Dynamic Sequences”. In: Algorithms - ESA 2015. Ed. by N. Bansal and I. Finocchi. Vol. 9294. Berlin, Heidelberg: Springer Berlin Heidelberg, pp. 891–902. DOI: 10.1007/978-3-662-48350-3_74. Navarro, G. (2022). Indexing Highly Repetitive String Collections. DOI: 10.48550/arXiv.2004.02781. arXiv: 2004.02781 [cs.DS]. URL: http://arxiv.org/abs/2004.02781. Navarro, G. and Y. Nekrich (2014). “Optimal Dynamic Sequence Representations”. In: SIAM Journal on Computing 43.5, pp. 1781–1806. DOI: 10.1137/130908245. Nishimoto, T., S. Kanda, and Y. Tabei (2022). “An Optimal-Time RLBWT Construction in BWT-Runs Bounded Space”. In: 49th International Colloquium on Automata, Languages, and Programming (ICALP 2022). Ed. by M. Bojańczyk, E. Merelli, and D. P. Woodruff. Vol. 229. Leibniz International Proceedings in Informatics (LIPIcs). Dagstuhl, Germany: Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 99:1–99:20. DOI: 10.4230/LIPIcs.ICALP.2022.99. Ohno, T., K. Sakai, Y. Takabatake, T. I, and H. Sakamoto (2018). “A Faster Implementation of Online RLBWT and Its Application to LZ77 Parsing”. In: Journal of Discrete Algorithms 52–53, pp. 18–28. DOI: 10.1016/j.jda.2018.11.002. Pătrașcu, M. and E. D. Demaine (2005). Logarithmic Lower Bounds in the Cell-Probe Model. DOI: 10.48550/ARXIV.CS/0502041. URL: https://arxiv.org/abs/cs/0502041. Pătrașcu, M. and M. Thorup (2014). Dynamic Integer Sets with Optimal Rank, Select, and Predecessor Search. DOI: 10.48550/ARXIV.1408.3045. URL: https://arxiv.org/abs/1408.3045. Policriti, A. and N. Prezza (2016). “Computing LZ77 in Run-Compressed Space”. In: 2016 Data Compression Conference (DCC). Snowbird, UT, USA: IEEE, pp. 23–32. DOI: 10.1109/DCC.2016.30. ## Appendix A. Proofs for Section 2 Proof of Lemma 2 If $H$ is empty, then so is $P$, $\text{occ}_H(Pc) = 0$, and the factorization of $Hc = c$ is the single new phrase $c$, as claimed. Assume now that $H$ is nonempty, and let $\ell := |H| - |P|$ be the starting position of $P$. The phrases of $H$ before $P$ are also phrases of $Hc$. Indeed, let $f_j$ be such a phrase, starting at $\ell_j$. Then $f_j$ followed by the next symbol of $H$ has no occurrence starting before $\ell_j$ in $H$, and hence none in $Hc$, since such an occurrence would end before $\ell_j + |f_j| + 1 \leq |H|$. So $f_j$ is still the longest prefix with an earlier occurrence (or a new symbol). It remains to find the phrases of $Hc$ starting at $\ell$. An occurrence of $Pc$ starting at a position $j < \ell$ ends at position $j + |P| \leq \ell - 1 + |P| = |H| - 1$, so it lies inside $H$. Conversely, every occurrence of $Pc$ in $H$ starts at a position $j \leq |H| - |P| - 1 < \ell$. Hence $Pc$ has an occurrence starting before $\ell$ if and only if $\mathrm{occ}_H(Pc) > 0$. If it does, then $Pc = (Hc)[\ell :]$ is the whole remaining suffix, and it is the phrase of $Hc$ starting at $\ell$ (note that if $P$ is a new symbol, it has no earlier occurrence and $\mathrm{occ}_H(Pc) = 0$). Otherwise, the phrase starting at $\ell$ is still $P$, and the next phrase starts at $|H|$. It consists of the single remaining symbol $c$, either as a new symbol or as the longest prefix of $c$ with an earlier occurrence. Corollary A.1 (Prefixes) Let $u \in \Sigma^*$ and $0 \leq k \leq |u|$. The factorization of $u[:k]$ consists of the phrases of $u$ that end by position $k$, followed by a prefix of the next phrase (if $k$ falls inside it). In particular, $z(u[:k]) \leq z(u)$. Proof We show that the phrases of $u[:k]$ start exactly at the starting positions of phrases of $u$ that are smaller than $k$. Fix $k$, and proceed by induction on $|u| \geq k$. For $|u| = k$ there is nothing to prove. If $|u| > k$, write $u = Hc$ with $c \in \Sigma$, so that $|H| \geq k$ and $H[:k] = u[:k]$. By Lemma 2, the phrases of $u$ start at the same positions as the phrases of $H$, and possibly also at $|H| \geq k$. Hence $u$ and $H$ have the same phrase starting positions below $k$, and by the induction hypothesis for $H$, these are the starting positions of the phrases of $u[:k]$. Proof of Theorem 5 Let $x = f_0 \cdots f_{z-1}$ be the LZ77 factorization of $x$, and let $\Pi$ be majority-respecting. We show that $\Pi$ makes at most $\log_2 n + 1$ mistakes while predicting the symbols of a single phrase $f = f_i$, which starts at position $\ell := |f_0 \cdots f_{i-1}|$. We charge one mistake to its first symbol $f[0] = x[\ell]$ (this also covers the prediction on the empty history, for $i = 0$). If $|f| = 1$ we are done. Otherwise, $f$ has a source: a position $p < \ell$ with $x[p : p + |f|] = f$. For $1 \leq k \leq |f|$, letV_{k}:=\left{j<\ell\mid x[j:j+k]=f[:k]\right},
so that $p \in V_k$ for all $k$, and $|V_1| \leq \ell < n$. Fix $1 \leq k < |f|$, and consider the prediction of $x[\ell + k] = f[k]$, which is made after the history $H := x[:\ell + k]$. By Corollary A.1, the factorization of $H$ is $f_0 \cdots f_{i-1}$ followed by $f[:k]$, so the last phrase of $H$ is $f[:k]$. An occurrence of $f[:k]$ in $H$, for $a \in \Sigma$, has length $k + 1$ and therefore starts at a position $j \leq \ell - 1$. Conversely, for every $j \in V_k$, the symbol $x[j + k]$ lies in $H$ since $j + k < \ell + k$. Hencev_{H}(f[:k],a)=\left|{j\in V_{k}\mid x[j+k]=a}\right|\quad\mathrm{f o re v e r y}a\in\Sigma,
1\leq|V_{[f]}|\leq2^{-m}|V_{1}|<2^{-m}n,
so $m < \log_{2} n$. Together with the first symbol, at most $\log_{2} n + 1$ mistakes are made on $f$, and summing over the $z$ phrases proves the theorem. ## Appendix B. Proofs for Section 3 Throughout, $L = c_1^{e_1} \cdots c_r^{e_r}$, and run $j$ occupies the positions $[E_{j-1}, E_j)$ of $L$, where $E_j := e_1 + \cdots + e_j$. Proof of Lemma 8 Consider insert $(i, c)$. If $L[i-1] = c$ or $L[i] = c$ (for the positions that exist), the new symbol extends the run containing that position. Otherwise, if $L[i-1]$ and $L[i]$ belong to the same run, of a symbol $c' \neq c$, the insertion splits it into two runs of $c'$ with the new run $c^1$ between them. Otherwise, $i$ is a boundary between two runs of symbols other than $c$, or an end of $L$, and the insertion creates a new run $c^1$ there. In all cases, no two runs of the same symbol become adjacent, so no runs merge, and no run disappears. Proof of Lemma 11 We use the structure described in Section 3.1: the partial-sums structures $A$ and $A_c$ over the run lengths, the run-head string $L'$, and the dictionary $F$ of active symbols. Components. The structures $A$, $A_c$ and $F$ are worst-case balanced search trees (e.g. red-black trees) with one node per entry, augmented with subtree sizes and subtree sums. They support prefix sums, searches, updates and insertions of entries in $O(\log r)$ worst-case time. The nodes of $A$ also store the run heads, and the node of each active symbol $c$ in $F$ stores its frequency and a pointer to $A_c$. Each node takes $O(1)$ words. For the run-head string $L'$ we use Navarro and Nekrich (2014, Theorem 10.5): a dynamic string of length $m$ over a general alphabet, with $s$ distinct symbols, can be stored in $mH_0 + o(m \log s) + O(s \log m)$ bits plus the space of a dictionary of its symbols, supporting rank in $O(T(s) + \log m)$ worst-case time and insertions in $O(U(s) + \log m)$ worst-case time, where $T(s)$ and $U(s)$ are the search and insertion times of the dictionary. We use a balanced search tree as the dictionary, so that $T(s), U(s) = O(\log s)$ and new symbols can be added at any time. Here $m = r$ and $s \leq r$, so both operations take $O(\log r)$ time, and the space is $O(r \log r)$ bits plus $O(r)$ words. The structure is described for words of $\Theta(\log m)$ bits. Since $r \leq n$ and $w = \Omega(\log n)$, each of its words fits in $O(1)$ of our words, so it occupies $O(r)$ words, and each of its word operations can be simulated in constant time. Its length-recalibration mechanism keeps the bounds worst-case as $r$ grows, including the construction of its lookup tables (Navarro and Nekrich, 2014, Section 9.3), and instances of constant size can be handled directly. Queries. $\operatorname{rank}_c(L,0) = 0$, and if $c$ is not in $F$ then $\operatorname{rank}_c(L,i) = 0$ for all $i$. Otherwise, Equation (2) uses one search and one prefix sum in $A$, one rank query on $L'$ and one prefix sum in $A_c$; the run head $L'[k]$ is stored in the node of $A$ found by the search. The query $C_L(c)$ is a prefix sum over the keys of $F$ smaller than $c$, which also works when $c$ is not active. Both take $O(\log r)$ time. Insertions. For insert $(i,c)$, we find the runs containing the positions $i-1$ and $i$ (if they exist) by searches in $A$, and their symbols in the nodes of $A$. If $c$ is not active, we first create an empty tree $A_c$ and insert $c$ into $F$ (with frequency 0) and into the dictionary of $L'$. We then perform the edit of Lemma 8. The index of a run $j$ of $c$ among the runs of $c$ is $\operatorname{rank}_c(L', j-1) + 1$, and similarly for other symbols. * Extending the run $j$ of $c$ next to position $i$ adds one to entry $j$ of $A$ and to entry $\operatorname{rank}_c(L', j-1) + 1$ of $A_c$. * Splitting run $j$, of a symbol $c' \neq c$, at offset $\tau := i - E_{j-1}$ replaces entry $j$ of $A$ by the three entries $\tau, 1, e_j - \tau$, and entry $\operatorname{rank}_{c'}(L', j-1) + 1$ of $A_{c'}$ by the two entries $\tau, e_j - \tau$. It inserts a new entry $\operatorname{rank}_c(L', j) + 1$, equal to 1, into $A_c$, and inserts the two symbols $cc'$ after the first $j$ symbols of $L'$. * A new run $c^1$ after the first $j$ runs ($0 \leq j \leq r$) is inserted as a new entry $j+1$ of $A$ and a new entry $\operatorname{rank}_c(L', j) + 1$ of $A_c$, both equal to 1, and $c$ is inserted after the first $j$ symbols of $L'$ (at the beginning if $j=0$). Finally, the frequency of $c$ in $F$ grows by one. These are $O(1)$ operations of $O(\log r)$ time each. Space. If $c$ has $r_c$ runs, then $A_c$ has $r_c$ nodes and its root is referenced from the node of $c$ in $F$; since $\sum_c r_c = r$, the trees $A$, $A_c$ and $F$ occupy $O(r)$ words, as does $L'$. Their operations use $O(\log r)$ words of temporary storage, which is also $O(r)$. Proof of Lemma 12 Bille et al. (2018, Theorem 2) prove a more general result with a parameter $\delta$. Their structure maintains a sequence of $s$ w-bit integers in linear space, and supports sum and search, changing an entry by less than $2^{\delta}$, inserting and deleting entries smaller than $2^{\delta}$, merging two consecutive entries, and dividing an entry, in $O(1 + \log s / \log(w/\delta))$ time per operation; the proof in their Section 3.2 gives worst-case bounds. For $\delta = 1$, adding one to an entry and inserting an entry equal to 0 or 1 are among these operations, and the time per operation is $O(1 + \log s / \log w)$. Proof of Lemma 13 We use the structure described in Section 3.2. It consists of the structures $A$ and $W_c$ of Lemma 12, where $A$ stores the run lengths and $W_c$, for each of the $\sigma$ symbols $c$, stores the lengths of the runs of $c$ and zeros for the other runs, and of the array $\text{cnt}$ with $\text{cnt}[y] = C_L(y)$ for every symbol $y$. Initially, these are $\sigma + 1$ empty structures and $\sigma$ zeros. The entries of $A$ are positive, so $A.\text{search}(i)$ is the run containing $L[i-1]$ for $0 < i \leq N$, and the formula for $\operatorname{rank}_c(L, i)$ in Section 3.2 uses four operations of Lemma 12 on sequences of length $r$. This takes $O(1 + \log r / \log w)$ time. The value $C_L(c) = \text{cnt}[c]$ takes $O(1)$ time. For insert$(i,c)$, we find the runs containing the positions $i-1$ and $i$ (if they exist) by searches in $A$, and the symbol of each such run $j$ by testing $W_y.\text{sum}(j) > W_y.\text{sum}(j-1)$ for all $y \in \Sigma$. This takes $O(\sigma)$ operations of Lemma 12. We then perform the edit of Lemma 8 in all $\sigma+1$ structures at once: * extending the run $j$ of $c$ next to position $i$ adds one to entry $j$ of $A$ and of $W_c$; * splitting run $j$, of a symbol $c' \neq c$, at offset $\tau := i - E_{j-1}$ divides entry $j$ into $\tau$ and $e_j - \tau$ in $A$ and in $W_{c'}$, and into two zeros in every other $W_y$; it then inserts a new entry $j+1$, equal to 1 in $A$ and in $W_c$ and to 0 in every other $W_y$; * a new run $c^1$ after the first $j$ runs is inserted as a new entry $j+1$, equal to 1 in $A$ and in $W_c$ and to 0 in every other $W_y$. Finally, $\text{cnt}[y]$ grows by one for every $y > c$, in $O(\sigma)$ time. The edit uses $O(1)$ operations of Lemma 12 in each of the $\sigma+1$ structures, so an insertion takes $O(\sigma)$ such operations and $O(\sigma(1 + \log r / \log w))$ time. By Lemma 12, each of the $\sigma+1$ sequences takes $O(r+1)$ words. Together with the $\sigma$-entry array $\text{cnt}$, the space is $O(\sigma(r+1))$ words. Proof of Lemma 15 We augment every node $v$ of the balanced tree $A$ of Lemma 11 with the summary\text{summary}(v) := \left( \text{summary}(v_{\text{L}}) \star (c_v, e_v) \right) \star \text{summary}(v_{\text{R}}),
where $c_v^{e_v}$ is the run stored at $v$, $v_L$ and $v_R$ are its children, and the summary of an empty subtree is $\bot$. By Lemma 14 and induction, summary(v) is valid for the multiset of symbols in the runs of the subtree of $v$. A summary takes two words, since its balance is at most $N < 2^w$. It depends only on the fields of $v$ and of its children. Hence, as for subtree sums, it can be maintained under insertions, including rebalancing, in $O(\log r)$ worst-case time. Algorithm B.1 answers a query candidate $(a,b)$. It handles the two partial runs at the ends of $L[a:b]$ separately, so that its recursive procedures only deal with whole runs. These procedures use the subtree sizes stored in $A$: size $(v)$ is the number of runs in the subtree of $v$ (zero for an empty subtree), and the rank of a run in this subtree is the number of runs before it there, so that the ranks are $0,\ldots,\text{size}(v)-1$ in the order of $L$. As $\star$ is not associative, we read $s_1\star s_2\star s_3$ as $(s_1\star s_2)\star s_3$; any other order would also do. Algorithm B.1: The query candidate $(a,b)$ in the augmented tree $A$ candidate $(a,b)$ 2 if $a = b$ then return $\bot$ 3 find the runs $j_a$ and $j_b$ containing $L[a]$ and $L[b-1]$, by searches in $A$ 4 if $j_a = j_b$ then return $c_{ja}$ 5 $s \leftarrow (c_{ja}, E_{ja} - a) \star \text{RUNS}(\text{root}, j_a, j_b - 1) \star (c_{jb}, b - E_{jb} - 1)$ 6 if $s = \bot$ then return $\bot$ 7 else return the symbol $c$ of $s = (c, p)$ 8 RUNS $(v, x, y)$ // the runs of ranks $x, \ldots, y-1$ in the subtree of $v$ 9 if $x = y$ then return $\bot$ 10 $k \leftarrow \text{size}(v_L)$ // the rank of the run of $v$ 11 if $y \leq k$ then return RUNS $(v_L, x, y)$ 12 if $x > k$ then return RUNS $(v_R, x - k - 1, y - k - 1)$ 13 return SUFFIX $(v_L, x) \star (c_v, e_v) \star \text{PREFIX}(v_R, y - k - 1)$ // $x \leq k < y$ 14 PREFIX $(v, y)$ // the runs of ranks $0, \ldots, y-1$ 15 if $y = 0$ then return $\bot$ 16 $k \leftarrow \text{size}(v_L)$ 17 if $y \leq k$ then return PREFIX $(v_L, y)$ 18 return summary $(v_L) \star (c_v, e_v) \star \text{PREFIX}(v_R, y - k - 1)$ 19 SUFFIX $(v, x)$ // the runs of ranks $x, \ldots, \text{size}(v) - 1$ 20 if $x = \text{size}(v)$ then return $\bot$ 21 $k \leftarrow \text{size}(v_L)$ 22 if $x > k$ then return SUFFIX $(v_R, x - k - 1)$ 23 return SUFFIX $(v_L, x) \star (c_v, e_v) \star \text{summary}(v_R)$ Correctness. Every call has $0 \leq x \leq y \leq \text{size}(v)$ for RUNS, $0 \leq y \leq \text{size}(v)$ for PREFIX, and $0 \leq x \leq \text{size}(v)$ for SUFFIX, so a call on an empty subtree returns $\bot$ in its first line. In the other cases, the runs in question either lie in one subtree of $v$, where the ranks in the right subtree are shifted by $k + 1$, or consist of a suffix of the runs of $v_L$, the run of $v$, and a prefix of the runs of $v_R$. By induction on the height of $v$ and Lemma 14, each procedure returns a valid summary of the multiset of symbols in its runs. In candidate $(a, b)$ with $j_a < j_b$, the string $L[a : b]$ consists of the last $E_{j_a} - a$ symbols of run $j_a$, the runs $j_a + 1, \ldots, j_b - 1$, which have ranks $j_a, \ldots, j_b - 2$ in $A$, and the first $b - E_{j_b-1}$ symbols of run $j_b$. Hence $s$ is valid for $L[a : b]$, and by Lemma 14 it is $(c, p)$ with $p \geq 1$ whenever $c$ has a strict majority in $L[a : b]$. Time. Every call of PREFIX or SUFFIX takes $O(1)$ time besides its recursive call, of which there is at most one, on a child of its node. Hence each of them visits the nodes of one downward path in $A$. The same holds for RUNS, except in its last case, at a node $u$, where it calls SUFFIX on $u_L$ and PREFIX on $u_R$ once each and stops. So the nodes visited by a query lie on two downward paths from the root, which agree down to $u$ and each have $O(\log r)$ nodes, besides the two searches. It takes $O(\log r)$ time and, for the recursion, $O(\log r)$ words of temporary space. The augmentation adds $O(1)$ words per node. ## Appendix C. Proofs for Section 4 Proof of Lemma 16 (1) The string $L^-$ is $L$ without the cell $p_\mathbb{S}$: the positions before $p_\mathbb{S}$ keep their index, and the index of each position after it decreases by one. This gives the formula for $\text{rank}_c$ with $c \in \Sigma$. Since $\mathbb{S} < c$, $C_L(c)$ counts the sentinel in addition to the symbols of $L^-$ that are smaller than $c$. (2) The suffixes of $cY$ are $cY$ itself and the suffixes of $Y$, whose relative order does not change. The BWT symbol of the row of $Y$, which is the row $p_{\mathbb{S}}$, changes from $\mathbb{S}$ to $c$; in $L^{-}$, this inserts $c$ at position $p_{\mathbb{S}}$, since the cells of $L$ before $p_{\mathbb{S}}$ are the first $p_{\mathbb{S}}$ cells of $L^{-}$. The new suffix $cY$ has BWT symbol $\mathbb{S}$, so its row changes $p_{\mathbb{S}}$ but not $L^{-}$. Its row is the number of suffixes of $Y$ that are smaller than $cY$. These are the suffixes starting with a symbol smaller than $c$, of which there are $C_L(c)$ (the first symbols of the suffixes of $Y$ are exactly the symbols of $L$), and the suffixes $cZ$ with $Z < Y$. The latter correspond to the suffixes $Z$ of $Y$ whose row precedes $p_{\mathbb{S}}$ and whose BWT symbol is $c$, of which there are $\text{rank}_c(L, p_{\mathbb{S}})$. By (1), $C_L(c) + \text{rank}_c(L, p_{\mathbb{S}}) = s$. The costs follow directly. Proof of Lemma 17 If $H$ is empty, then $P = \varepsilon$, $Y = L = \mathbb{S}$, $I = [0,1)$ and $p_{\mathbb{S}} = 0$. All $\nu_c$ and $v_H(P,c)$ are zero, and $b - a - 1 = 0$, so the claims hold. Assume now that $H$ is nonempty, and write $t = |H|$ and $k = |P| \geq 1$. The occurrences $H[j : j + k] = P$ correspond bijectively to the occurrences of $P^R$ in $Y = H^R\mathbb{S}$, at the positions $q = t - j - k$. If $j + k < t$, then $q > 0$, and the symbol following the occurrence in $H$ is $H[j + k] = Y[q - 1]$, which is the BWT symbol of the row of the suffix $Y[q : ]$. So, for $c \in \Sigma$, the rows of $[a,b)$ with BWT symbol $c$ correspond to the occurrences of $P$ in $H$ that are followed by $c$, and $\nu_c = \text{occ}_H(Pc) = v_H(P,c)$. The remaining occurrence, with $j + k = t$, is the suffix $P$ of $H$. It has $q = 0$, so its row is the row of $Y$ itself, namely $p_{\mathbb{S}}$, whose BWT symbol is $\mathbb{S}$. Hence $a \leq p_{\mathbb{S}} < b$, and the other $b - a - 1$ rows of the interval have symbols in $\Sigma$. Proof of Theorem 18 We show by induction that after every update, the structure stores $L^{-}$ and $p_{\mathbb{S}}$ for $Y = H^{R}\mathbb{S}$, the list contains the active symbols of $H$, and $I = I_{Y}(P^{R})$ for the last phrase $P$ of $H$. The first two claims follow from Lemma 16, and from the fact that $c$ is new if and only if $\text{rank}_{c}(L,N) = 0$, since $L$ and $Y$ contain the same symbols. Initially, $I = [0,1) = I_{\mathbb{S}}(\varepsilon)$. On an update, $J$ is nonempty if and only if $cP^R$ occurs in $Y$. Since $cP^R$ does not contain $\mathbb{S}$, this happens if and only if $Pc$ occurs in $H$. By Lemma 2, the new last phrase is therefore $Pc$ if $J$ is nonempty, and $c$ otherwise. In the first case, the interval of $cP^R$ in the new text consists of the rows of the old suffixes in $J$ and of the new suffix $cY$, which starts with $cP^R$. The rows of the old suffixes keep their order, and the index of each one after the new row $s$ increases by one; hence the interval is $[a', b'+1)$. In the second case, the interval $I_{cY}(c)$ is computed directly. By Lemma 17, the values $\nu_c$ are the votes of LZP. On a nonempty history the list of active symbols is nonempty, and since only active symbols can have a positive vote, an active symbol with the largest $\nu_c$ maximizes $v_H(P,c)$ over $\Sigma$, as LZP requires. On the empty history all symbols are tied. The mistake bound then follows from Theorem 5, since LZP is majority-respecting. An update performs at most two backward steps (each one $C_L$ query and two rank queries), one rank query for the test of a new symbol, and one prepend, which takes $O(T_{\text{rank}} + T_{\text{count}} + T_{\text{ins}})$ time by Lemma 16. A prediction computes two rank queries for each active symbol. With caching, the update also computes the next prediction. Besides the structure, the state consists of $O(1)$ words and the list of active symbols. Proof of Theorem 19 The update and the invariant are those of RLBWT-LZP. Deleting the row $p_{\}$ $\in [a,b)$ from $L$ leaves the other rows of $[a,b)$ at the positions $[a,b-1)$ of $L^-$. By Lemma 17, the multiset of symbols of $L^-[a:b-1]$ contains each $c \in \Sigma$ exactly $v_H(P,c)$ times, and has size $b-a-1 = v_H(P)$. If $v_H(P,c) > \frac{1}{2}v_H(P)$, then $c$ occurs in $L^-[a:b-1]$ more than $(b-a-1)/2$ times, so the candidate query returns $c$, which is predicted. Hence the predictor is majority-respecting, and Theorem 5 applies. A prediction makes one candidate query, and the state has $O(1)$ words besides the structure. Proof of Lemma 20 For a string $u$ and $k \geq 1$, let $d_k(u)$ be the number of distinct substrings of $u$ of length $k$. The substring complexity of $u$ is $\delta(u) := \max_{1 \leq k \leq |u|} d_k(u)/k$. We prove the stronger boundr_{\max}(x)=O(\delta(x)\log\delta(x)\log n),
which implies the lemma since $\delta(x) \leq z(x)$ (see Navarro, 2022, Sections 3.3, 3.9 and 3.10). Let $T$ be a text of length $m$ whose last symbol is a unique smallest sentinel $\$. Kempa and Kociumaka (2020) use a cyclic version of the substring complexity: for $k \geq 1$, let $d'_k(T)$ be the number of distinct substrings of length $k$ of the infinite string $TTT\cdots$, and let $\delta'(T) := \max_{1 \leq k \leq m} d'_k(T)/k$. For $k \leq m$, every substring of $T$ of length $k$ is also a substring of $TTT\cdots$, and a substring of $TTT\cdots$ of length $k$ that is not a substring of $T$ contains $\$ at one of its first $k-1$ positions, and is determined by that position. Hence $d_k(T) \leq d'_k(T) \leq d_k(T) + k-1$, and $\delta(T) \leq \delta'(T) \leq \delta(T) + 1$. Writing $\delta'$ for $\delta'(T)$, Kempa and Kociumaka (2020, Theorem III.7) show thatr(\mathrm{BWT}(T))=O\Big(\delta'\log\delta'\cdot\max\Big{1,\log\frac{m}{\delta'\log\delta'}\Big}\Big)=O(\delta'\log\delta'\log m),
and their BWT coincides with ours. Let $T:=x[:t]^{R}\$ for some $1\leq t\leq n$ (for $t=0$, the BWT has one run). Then $\delta(T)\geq 2$, since $T$ has at least two distinct symbols. The only substring of $T$ of length $k$ that contains $\$ is its suffix, so $d_k(T)\leq d_k(x[:t]^{R})+1$ and $\delta(T)\leq \delta(x[:t]^{R})+1$. Moreover, $\delta$ is invariant under reversal, and $\delta(x[:t])\leq \delta(x)$ since every substring of $x[:t]$ is a substring of $x$. Hence $2\leq \delta' \leq \delta(x)+2\leq 3\delta(x)$, and $r(\mathrm{BWT}(T))=O(\delta(x)\log\delta(x)\log n)$, using $m=t+1\leq n+1$. Consequently, the time and space bounds in Corollary 21 also hold with $z(x)$ replaced by $\delta(x)\leq z(x)$; the mistake bound remains $z(x)(\log_2 n+1)$.