This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

11institutetext: School of Intelligent Systems Engineering, Sun Yat-Sen University, Shenzhen, China 11email: luorx,[email protected],11email: [email protected]

String Rearrangement Inequalities and
a Total Order Between Primitive Words thanks: Corresponding author: Kai Jin (). Supported by National Natural Science Foundation of China 62002394.

Ruixi Luo 11 0000-0003-0483-0119   
Taikun Zhu
11 0000-0001-7365-9576
  
Kai Jin
11 0000-0003-3720-5117
Abstract

We study the following rearrangement problem: Given nn words, rearrange and concatenate them so that the obtained string is lexicographically smallest (or largest, respectively). We show that this problem reduces to sorting the given words so that their repeating strings are non-decreasing (or non-increasing, respectively), where the repeating string of a word AA refers to the infinite string AAAAAA\ldots. Moreover, for fixed size alphabet Σ\Sigma, we design an O(L)O(L) time sorting algorithm of the words (in the mentioned orders), where LL denotes the total length of the input words. Hence we obtain an O(L)O(L) time algorithm for the rearrangement problem. Finally, we point out that comparing primitive words via comparing their repeating strings leads to a total order, which can further be extended to a total order on the finite words (or all words).

Keywords:
String rearrangement inequalities Primitive words Combinatorics on words String ordering Greedy algorithm.

1 Introduction

Combinatorics on words (MSC: 68R15) have strong connections to many fields of mathematics and have found significant applications to theoretical computer science and molecular biology (DNA sequences) [10, 17, 21, 5, 8, 14]. Particularly, the primitive words over some alphabet Σ\Sigma have received special interest, as they have applications in the formal languages and algebraic theory of codes [19, 12, 13, 16]. A word is primitive if it is not a proper power of a shorter word.

In this paper, we consider the following rearrangement problem of words: Given nn words A1,,AnA_{1},\ldots,A_{n}, rearrange and concatenate these words so that the obtained string SS is lexicographically smallest (or largest, respectively). We prove that the lexicographical smallest outcome of SS happens when the words are arranged so that their repeating strings are increasing, and the largest outcome of SS happens when the words are arranged reversely; see Lemma 6. Throughout, the repeating string of a word AA refers to the infinite string R(A)=AAAR(A)=AAA\ldots.

Based on the above lemma (we suggest to name its results as “string rearrangement inequalities”), the aforementioned rearrangement problem reduces to sorting the words A1,,AnA_{1},\ldots,A_{n} so that R(A1)R(An)R(A_{1})\leq\ldots\leq R(A_{n}). We show how to sort for the special case where A1,,AnA_{1},\ldots,A_{n} are primitive and distinct in O(i|Ai|)O(\sum_{i}|A_{i}|) time. The general case can be easily reduced to the special case and can be solved in the same time bound. Note that we assume bounded alphabet Σ\Sigma and the size of Σ\Sigma is fixed. Moreover, |X||X| always denotes the length of word XX.

Our algorithm beats the plain algorithm based on sorting (via comparing several pairs R(Ai),R(Aj)R(A_{i}),R(A_{j})) by a factor of logn\log n. The algorithm is simple – it only applies basic data structures such as tries and the failure function [10]. Nevertheless, its correctness and running time analysis is non-straightforward.

We mention that comparing primitive words via comparing their repeating strings leads to a total order \leq_{\infty} on primitive words, which can extended to a total order \leq_{\infty} on all words (section 5). We show that this order is the same as the lexicographical order over Lyndon words but are different over primitive words and finite words. It is also different from reflected lexicographic order, co-lexicographic order, shortlex order, Kleene-Brouwer order, V-Order, alternative order [11, 9, 3, 2, 1]. It seems that order \leq_{\infty} has not been reported in literature.

1.1 Related work

It is shown in [6] that the language of Lyndon words is not context-free. Also, many people conjectured that the language of primitive words is not context-free [12, 19, 6, 13]. But this conjecture is unsettled thus far, to the best of our knowledge. It would be interesting to explore whether the results shown in this paper can be helpful for solving this longstanding open problem in the future. See more introductions about primitive words in [16].

Fredricksen and Maiorana [15] showed that if one concatenates, in lexicographic order, all the Lyndon words that have length dividing a given number nn, the result is a de Bruijn sequence. Au [4] further showed that if “dividing nn” is replaced by “identical to nn”, the result is a sequence which contains exactly once every primitive word of length nn as a factor. Note that concatenating some Lyndon words by lexicographic order is the same as concatenating by \leq_{\infty} order.

The Lyndon words have many interesting properties and have found plentiful applications, both theoretically and practically. Among others, they are used in constructing de Brujin sequence as mentioned above (which have found applications in cryptography), and they are applied in proving the “runs theorem” [20, 5, 8]. The famous Chen-Fox-Lyndon Theorem states that any word WW can be uniquely factorized into W=W1W2WmW=W_{1}W_{2}\ldots W_{m}, such that each WiW_{i} is a Lyndon word, and W1WmW_{1}\geq\ldots\geq W_{m} [14, 7] (Here \geq refers to the opposite of Lexicographical order, but is the same as the opposite of \leq_{\infty}). This factorization is used in the computation of runs in a word [8]. See the Bible of combinatorics on words [17] for more introductions about Lyndon words and primitive words.

2 Preliminaries

Definition 1

The nthn^{th} power of word AA is defined as:

An={AAn1,n>0;empty word,n=0.A^{n}=\left\{\begin{array}[]{ccc}AA^{n-1},&n>0;\\ \mbox{\emph{empty word}},&n=0.\\ \end{array}\right.

A word AA is non-primitive if it equals BkB^{k} for some word BB and integer k2k\geq 2. Otherwise, AA is primitive. (By this definition the empty word is not primitive.)

The next lemma summarizes three results about the powers proved by Lyndon and Schüzenberger [18]; see their Lemmas 3 and 4, and Corollary 4.1. (More introductions of these results can be found in Section 1.3 “Conjugacy” of [17].)

Lemma 1

[18] Given words AA and BB, there exist C,k,lC,k,l such that A=CkA=C^{k} and B=ClB=C^{l} when one of the following conditions holds:

  1. 1.

    AB=BAAB=BA.

  2. 2.

    Two powers Am1A^{m_{1}} and Bm2B^{m_{2}} have a common prefix of length |A|+|B||A|+|B|.

  3. 3.

    Am1=Bm2A^{m_{1}}=B^{m_{2}}.

Definition 2

The root of a word AA, denoted by 𝗋𝗈𝗈𝗍(A)\mathsf{root}(A), is the unique primitive word BB such that AA is a power of BB. The uniqueness of root is obvious, a formal proof can be found in Corollary 4.2 of [18] or in [16].

Lemma 2

Assume AA is a non-empty word. Find the largest j<|A|j<|A| such that the prefix of AA with length jj equals the suffix of AA with length jj. Let k=|A|j>0k=|A|-j>0. Then,

|𝗋𝗈𝗈𝗍(A)|={k,|A|=0(modk);|A|,|A|0(modk).|\mathsf{root}(A)|=\left\{\begin{array}[]{clc}k,&{}&\hbox{$|A|=0\pmod{k}$;}\\ |A|,&{}&\hbox{$|A|\neq 0\pmod{k}$.}\end{array}\right. (1)
Proof

This result should be well-known. A simple proof is as follows.

Fact 1. If S=BB=BBS=BB^{\prime}=B^{\prime}B and B,BB,B^{\prime} are non-empty, SS is non-primitive.

This is a trivial fact and is implied by Lemma 1 (condition 1); proof omitted.

Claim 1. |𝗋𝗈𝗈𝗍(A)|k|\mathsf{root}(A)|\geq k.

Proof: The prefix and suffix of AA with length |A||𝗋𝗈𝗈𝗍(A)||A|-|\mathsf{root}(A)| are the same, which implies that j|A||𝗋𝗈𝗈𝗍(A)|j\geq|A|-|\mathsf{root}(A)|. Consequently, |𝗋𝗈𝗈𝗍(A)||A|j=k|\mathsf{root}(A)|\geq|A|-j=k.

Claim 2. If |𝗋𝗈𝗈𝗍(A)|<|A||\mathsf{root}(A)|<|A| (i.e., AA is non-primitive), then k|𝗋𝗈𝗈𝗍(A)|k\geq|\mathsf{root}(A)|.

Proof: Denote S=𝗋𝗈𝗈𝗍(A)S=\mathsf{root}(A) and assume |S|<|A||S|<|A|. Therefore, A=Sd(d2)A=S^{d}~{}(d\geq 2). Suppose to the opposite that k<|𝗋𝗈𝗈𝗍(A)|k<|\mathsf{root}(A)|. Let BB be the prefix of SS with length kk, and BB^{\prime} be the suffix of SS such that S=BBS=BB^{\prime}. As k<|S|k<|S|, we have j>|A||S||S|j>|A|-|S|\geq|S|. Further since the suffix of AA with length jj (which starts with BBB^{\prime}B) equals to the prefix of AA with length jj (which starts with S=BBS=BB^{\prime}), we get S=BBS=B^{\prime}B. Applying Fact 1, 𝗋𝗈𝗈𝗍(A)=S\mathsf{root}(A)=S is non-primitive. Contradictory.

We are ready to prove the lemma. When |A||A| is a multiple of kk, AA is a power of its prefix of length kk, which means |𝗋𝗈𝗈𝗍(A)|k|\mathsf{root}(A)|\leq k. Further by Claim 1, |𝗋𝗈𝗈𝗍(A)|=k|\mathsf{root}(A)|=k. Next, assume |A||A| is not a multiple of kk. Since |A||A| is a multiple of |𝗋𝗈𝗈𝗍(A)||\mathsf{root}(A)|, we see |𝗋𝗈𝗈𝗍(A)|k|\mathsf{root}(A)|\neq k. Further by Claims 1 and 2, it follows that |𝗋𝗈𝗈𝗍(A)|=|A||\mathsf{root}(A)|=|A|. ∎

For a non-empty word AA, denote by R(A)R(A) the infinite repeating string AAAA\ldots.

Problem 1. Given non-empty words A1,,AnA_{1},\ldots,A_{n}, sort them so that

R(A1)R(An).R(A_{1})\leq\ldots\leq R(A_{n}).

Clearly, R(A)=R(𝗋𝗈𝗈𝗍(A))R(A)=R(\mathsf{root}(A)). To solve Problem 1, we can replace AA by 𝗋𝗈𝗈𝗍(A)\mathsf{root}(A) (using a preprocessing algorithm based on Lemma 2), and then it reduces to:

Problem 1’. Given primitive words A1,,AnA_{1},\ldots,A_{n}, sort them so that

R(A1)R(An).R(A_{1})\leq\ldots\leq R(A_{n}).
Definition 3

For any two non-empty words SS and AA, denote by degA(S)\deg_{A}(S) the largest integer dd so that SdS^{d} is a prefix of AA. Moreover, for non-empty word SS and set of non-empty words 𝒜={A1,,An}\mathcal{A}=\{A_{1},\ldots,A_{n}\}, denote deg𝒜(S)=maxjdegAj(S)\deg_{\mathcal{A}}(S)=\max_{j}\deg_{A_{j}}(S).

In other words, if we build the trie TT of 𝒜\mathcal{A}, Sdeg𝒜(S)S^{\deg_{\mathcal{A}}(S)} is the longest power of SS that equals to some path of the trie TT starting from its root.

For any i(1in)i~{}(1\leq i\leq n), denote

Ni\displaystyle N_{i} =\displaystyle= the deg𝒜(Ai)\deg_{\mathcal{A}}(A_{i})-th power of AiA_{i} (2)
Mi=NiAi2\displaystyle M_{i}=N_{i}A_{i}^{2} =\displaystyle= the (deg𝒜(Ai)\deg_{\mathcal{A}}(A_{i})+2)-th power of AiA_{i} (3)

The following lemma is fundamental to our algorithm.

Lemma 3

For non-empty words AA and BB, the relation between R(A)R(A) and R(B)R(B) is the same as the relation between ABAB and BABA. In other words,

R(A)<R(B)\displaystyle R(A)<R(B) \displaystyle\quad\Leftrightarrow AB<BA,\displaystyle\quad AB<BA, (4)
R(A)>R(B)\displaystyle R(A)>R(B) \displaystyle\quad\Leftrightarrow AB>BA.\displaystyle\quad AB>BA. (5)
R(A)=R(B)\displaystyle R(A)=R(B) \displaystyle\quad\Leftrightarrow AB=BA,\displaystyle\quad AB=BA, (6)
Proof

Assume that A,BA,B are words that consist of the decimal symbols ‘0’,…,‘9’. The proof can be easily extended to the more general case.

Let α,β\alpha,\beta denote the number represented by strings A,BA,B. For example, string ‘89’ represents number 89. Denote a=|A|a=|A| and b=|B|b=|B|. Observe that

AB<BAα10b+β<β10a+αα10a1<β10b1.AB<BA~{}\Leftrightarrow~{}\alpha\cdot 10^{b}+\beta<\beta\cdot 10^{a}+\alpha~{}\Leftrightarrow~{}\frac{\alpha}{10^{a}-1}<\frac{\beta}{10^{b}-1}.

Moreover,

α10a1=α110a1110a=α[110a+(110a)2+(110a)3+]=0.ααα=0.α˙;\frac{\alpha}{10^{a}-1}=\alpha\frac{\frac{1}{10^{a}}}{1-\frac{1}{10^{a}}}=\alpha[\frac{1}{10^{a}}+(\frac{1}{10^{a}})^{2}+(\frac{1}{10^{a}})^{3}+\ldots]=0.\alpha\alpha\alpha\cdots=0.\dot{\alpha};
β10b1=β110b1110b=β[110b+(110b)2+(110b)3+]=0.βββ=0.β˙.\frac{\beta}{10^{b}-1}=\beta\frac{\frac{1}{10^{b}}}{1-\frac{1}{10^{b}}}=\beta[\frac{1}{10^{b}}+(\frac{1}{10^{b}})^{2}+(\frac{1}{10^{b}})^{3}+\ldots]=0.\beta\beta\beta\cdots=0.\dot{\beta}.

So, AB<BA0.α˙<0.β˙R(A)<R(B).AB<BA\Leftrightarrow 0.\dot{\alpha}<0.\dot{\beta}\Leftrightarrow R(A)<R(B). Similarly, (5) and (6) hold. ∎

A more rigorous but complicated proof of Lemma 3 is given in the appendix.

As an interesting corollary of Lemma 3, we obtain that “if ABBAAB\leq BA and BCCBBC\leq CB, then ACCAAC\leq CA”. This transitivity is not obvious without Lemma 3.

3 A linear time algorithm for sorting the repeating words

Assume that A1,,AnA_{1},\ldots,A_{n} are primitive. Denote L=i|Ai|L=\sum_{i}|A_{i}| for short. This section presents an O(L)O(L) time algorithm for solving Problem 1’, that is, sorting R(A1),,R(An)R(A_{1}),\ldots,R(A_{n}). We start with two nontrivial observations.

Lemma 4

The relation between infinitely repeating strings R(Ai)R(A_{i}) and R(Aj)R(A_{j}) is the same as the relation between words MiM_{i} and MjM_{j}, that is,

R(Ai)=R(Aj)\displaystyle R(A_{i})=R(A_{j}) \displaystyle\quad\Leftrightarrow Mi=Mj,\displaystyle\quad M_{i}=M_{j},
R(Ai)<R(Aj)\displaystyle R(A_{i})<R(A_{j}) \displaystyle\quad\Leftrightarrow Mi<Mj,\displaystyle\quad M_{i}<M_{j},
R(Ai)>R(Aj)\displaystyle R(A_{i})>R(A_{j}) \displaystyle\quad\Leftrightarrow Mi>Mj.\displaystyle\quad M_{i}>M_{j}.

As a corollary, sorting R(A1),,R(An)R(A_{1}),\ldots,R(A_{n}) reduces to sorting M1,,MnM_{1},\ldots,M_{n}.

Proof

Consider the comparison of R(Ai)R(A_{i}) and R(Aj)R(A_{j}). Assume |Ai||Aj||A_{i}|\leq|A_{j}|. Otherwise it is symmetric.

First, consider the case R(Ai)=R(Aj)R(A_{i})=R(A_{j}). Let m1=|Aj|m_{1}=|A_{j}| and m2=|Ai|m_{2}=|A_{i}|. We know Aim1=Ajm2A_{i}^{m_{1}}=A_{j}^{m_{2}} because R(Ai)=R(Aj)R(A_{i})=R(A_{j}). Applying Lemma 1 (condition 3), Ai=CkA_{i}=C^{k} and Aj=ClA_{j}=C^{l} for some C,k,lC,k,l. Further since Ai,AjA_{i},A_{j} are primitive, Ai=C=AjA_{i}=C=A_{j}. It follows that Mi=MjM_{i}=M_{j}. Next, assume that R(Ai)R(Aj)R(A_{i})\neq R(A_{j}).

Let p=degAj(Ai)p=\deg_{A_{j}}(A_{i}). Thus, Aj=AipSA_{j}=A_{i}^{p}S, where p0p\geq 0 and AiA_{i} is not a prefix of SS. Be aware that pdeg𝒜(Ai)p\leq\deg_{\mathcal{A}}(A_{i}) by the definition of deg𝒜(Ai)\deg_{\mathcal{A}}(A_{i}).

According to Lemma 3, the comparison of R(Ai)R(A_{i}) and R(Aj)R(A_{j}) equals to the comparison of AiAjA_{i}A_{j} and AjAiA_{j}A_{i}. Further since Aj=AipSA_{j}=A_{i}^{p}S, it equals to the comparison of AipAiSA_{i}^{p}A_{i}S and AipSAiA_{i}^{p}SA_{i}. In the following, we discuss two subcases.

Subcase 1. |S|>|Ai||S|>|A_{i}|, or |S||Ai||S|\leq|A_{i}| and SS is not a prefix of AiA_{i}.

Recall that AiA_{i} is not a prefix of SS. In this subcase, we will find an unequal letter if we compare AiA_{i} with SS (starting from the leftmost letter). Comparing AipAiSA_{i}^{p}A_{i}S and AipSAiA_{i}^{p}SA_{i} is thus equivalent to comparing the prefixes AipAiA_{i}^{p}A_{i} and AipSA_{i}^{p}S.

Notice that AipAi=Aip+1A_{i}^{p}A_{i}=A_{i}^{p+1} and AipS=AjA_{i}^{p}S=A_{j} are also prefixes of MiM_{i} and MjM_{j}, respectively (note that Aip+1A_{i}^{p+1} is a prefix of MiM_{i} because MiM_{i} is the (deg𝒜(Ai)+2)(\deg_{\mathcal{A}}(A_{i})+2)-th power of AiA_{i} and deg𝒜(Ai)p\deg_{\mathcal{A}}(A_{i})\geq p as mentioned above). Therefore, comparing MiM_{i} and MjM_{j} is also equivalent to comparing the two prefixes AipAiA_{i}^{p}A_{i} and AipSA_{i}^{p}S.

Altogether, comparing R(Ai),R(Aj)R(A_{i}),R(A_{j}) is equivalent to comparing Mi,MjM_{i},M_{j}.

Subcase 2. SS is a prefix of AiA_{i}. (This means SS is a proper prefix of AiA_{i} as SAiS\neq A_{i}.)

Assume Ai=STA_{i}=ST. Comparing AipAiSA_{i}^{p}A_{i}S and AipSAiA_{i}^{p}SA_{i} is just the same as comparing AipSTSA_{i}^{p}STS and AipSSTA_{i}^{p}SST. It reduces to proving that comparing MiM_{i} and MjM_{j} also reduces to comparing AipSTSA_{i}^{p}STS and AipSSTA_{i}^{p}SST.

First, we argue that STTSST\neq TS. Suppose to the opposite that ST=TSST=TS. Applying Lemma 1 (condition 1), S=CkS=C^{k} and T=ClT=C^{l} for some C,k,lC,k,l. This implies that AiA_{i} and AjA_{j} are both powers of CC, and hence R(Ai)=R(Aj)R(A_{i})=R(A_{j}), contradictory.

Observe that AipSTSA_{i}^{p}STS is a prefix of AipSTST=Aip+2A_{i}^{p}STST=A_{i}^{p+2}, which is a prefix of MiM_{i} (because MiM_{i} is the (deg𝒜(Ai)+2)(\deg_{\mathcal{A}}(A_{i})+2)-th power of AiA_{i} and deg𝒜(Ai)+2p+2\deg_{\mathcal{A}}(A_{i})+2\geq p+2).

Observe that p>0p>0. Otherwise Aj=Ai0SA_{j}=A_{i}^{0}S is shorter than AiA_{i}, which contradicts our assumption |Ai||Aj||A_{i}|\leq|A_{j}|. As a corollary, AiA_{i} is a prefix of Aj=AipSA_{j}=A_{i}^{p}S. Therefore, AipSST=AipSAi=AjAiA_{i}^{p}SST=A_{i}^{p}SA_{i}=A_{j}A_{i} is a prefix of Aj2A_{j}^{2}, which is a prefix of MjM_{j}.

To sum up, MiM_{i} and MjM_{j} admit AipSTSA_{i}^{p}STS and AipSSTA_{i}^{p}SST as prefixes, respectively. Further since TSSTTS\neq ST, comparing Mi,MjM_{i},M_{j} reduces to comparing AipSTSA_{i}^{p}STS and AipSSTA_{i}^{p}SST, which is equivalent to comparing R(Ai),R(Aj)R(A_{i}),R(A_{j}) as mentioned above. ∎

Assume A1,,AnA_{1},\ldots,A_{n} are distinct henceforth in this section. To this end, we can use a trie to reduce those duplicate elements in A1,,AnA_{1},\ldots,A_{n}, which is trivial.

Lemma 5

When A1,,AnA_{1},\ldots,A_{n} are primitive and distinct, i|Ni|=O(L)\sum_{i}|N_{i}|=O(L).

Proof

First, we argue that N1,,NnN_{1},\ldots,N_{n} are distinct. Suppose that Ni=Nj(ij)N_{i}=N_{j}~{}(i\neq j). Recall that Ni=AimN_{i}=A_{i}^{m} (for m=deg𝒜(Ai)m=\deg_{\mathcal{A}}(A_{i})) and Nj=AjnN_{j}=A_{j}^{n} (for n=deg𝒜(Aj)n=\deg_{\mathcal{A}}(A_{j})). Applying Lemma 1 (condition 3), Ai=CkA_{i}=C^{k} and Aj=ClA_{j}=C^{l}. Further since Ai,AjA_{i},A_{j} are primitive, Ai=C=AjA_{i}=C=A_{j}, which contradicts the assumption that AiAjA_{i}\neq A_{j}.

We say NiN_{i} extremal if it is not a prefix of any word in {N1,,Nn}{Ni}\{N_{1},\ldots,N_{n}\}\setminus\{N_{i}\}. Partition N1,,NnN_{1},\ldots,N_{n} into several groups such that (a) for elements in the same group, one of them is the prefix of the other, and (b) the longest element in each group is extremal. (It is obvious that such a partition exists: we can first distribute the extremal ones to different groups, and then distribute the non-extremal ones to suitable group (each non-extremal one is a prefix of some extremal ones).

Now, consider any such group, e.g., Ni1,,NixN_{i_{1}},\ldots,N_{i_{x}}. It suffices to prove that (X) |Ni1|++|Nix|=O(|Ai1|++|Aix|)|N_{i_{1}}|+\ldots+|N_{i_{x}}|=O(|A_{i_{1}}|+\ldots+|A_{i_{x}}|), and we prove it in the following. Without loss of generality, assume that NijN_{i_{j}} is a prefix of Nij+1N_{i_{j+1}} for j<xj<x.

We state two important formulas: (i) Nix=AixN_{i_{x}}=A_{i_{x}}. (ii) |Nij|<|Aij|+|Aij+1||N_{i_{j}}|<|A_{i_{j}}|+|A_{i_{j+1}}| for j<xj<x. Equation (X) above follows from formulas (i) and (ii) immediately.

Proof of (i). Suppose to the contrary that NixAixN_{i_{x}}\neq A_{i_{x}}. By the definition of NixN_{i_{x}}, there exists some AjA_{j} such that NixN_{i_{x}} is a prefix of AjA_{j}. Clearly, jixj\neq i_{x} since NixN_{i_{x}} is not a prefix of AixA_{i_{x}}. Consequently, NixN_{i_{x}} is a prefix of some other NjN_{j}, which means NixN_{i_{x}} is not extremal, contradicting property (b) of the grouping mentioned above.

Proof of (ii). Suppose to the contrary that |Nij||Aij|+|Aij+1||N_{i_{j}}|\geq|A_{i_{j}}|+|A_{i_{j+1}}|. Because NijN_{i_{j}} and Nij+1N_{i_{j+1}} are powers of Ai,jA_{i,j} and Aij+1A_{i_{j+1}} and share a common prefix, NijN_{i_{j}}, of length at least |Aij|+|Aij+1||A_{i_{j}}|+|A_{i_{j+1}}|. By Lemma 1 (condition 2), Aij=CkA_{i_{j}}=C^{k} and Aij+1=ClA_{i_{j+1}}=C^{l} for some C,k,lC,k,l. Hence Aij=Aij+1A_{i_{j}}=A_{i_{j+1}}, as AijA_{i_{j}} and Aij+1A_{i_{j+1}} are primitive. Contradictory.

Our algorithm for sorting R(A1),,R(An)R(A_{1}),\ldots,R(A_{n}) is simply as follows.

First, we build a trie of A1,,AnA_{1},\ldots,A_{n} and use it to compute N1,,NnN_{1},\ldots,N_{n}. In particularly, for computing NiN_{i}, we walk along the trie from the root and search for maximal pieces of AiA_{i}, which takes O(|Ni|+|Ai|)=O(|Ni|)O(|N_{i}|+|A_{i}|)=O(|N_{i}|) time. The total running time for computing N1,,NnN_{1},\ldots,N_{n} is therefore O(i|Ni|)=O(L)O(\sum_{i}|N_{i}|)=O(L).

Second, we compute M1,,MnM_{1},\ldots,M_{n} and build a trie of them. By utilizing this trie, we obtain the lexicographic order of M1,,MnM_{1},\ldots,M_{n}, which equals the order of R(A1),,R(An)R(A_{1}),\ldots,R(A_{n}) according to Lemma 4. The running time of the second step is i|Mi|=i|Ni|+2i|Ai|=O(L)+O(L)=O(L)\sum_{i}|M_{i}|=\sum_{i}|N_{i}|+2\sum_{i}|A_{i}|=O(L)+O(L)=O(L).

To sum up, we obtain

Theorem 3.1

Problem 1’ can be solved in O(L)=O(i|Ai|)O(L)=O(\sum_{i}|A_{i}|) time.

In addition, we can solve Problem 1 within the same time bound.

Theorem 3.2

Problem 1 can be solved in O(L)=O(i|Ai|)O(L)=O(\sum_{i}|A_{i}|) time.

Proof

It remains to showing that 𝗋𝗈𝗈𝗍(Ai)\mathsf{root}(A_{i}) can be computed in O(|Ai|)O(|A_{i}|) time.

Applying Lemma 2, computing 𝗋𝗈𝗈𝗍(A)\mathsf{root}(A) reduces to finding the largest j<|A|j<|A| such that the prefix of AA with length jj equals the suffix of AA with length jj. Moreover, the famous KMP algorithm [10] finds this jj in O(|A|)O(|A|) time. ∎

As a comparison, there exists a less efficient algorithm for solving Problem 1, which is based on a standard sorting algorithm associated with a naïve gadget for comparing R(A)R(A) and R(B)R(B) – according to Lemma 3, comparing R(A)R(A) and R(B)R(B) reduces to comparing ABAB and BABA, which takes O(|A|+|B|)O(|A|+|B|) time. The time complexity of this alternative algorithm is higher. For example, when A1A_{1}=“aaaaaa1”, A2A_{2}=“aaaaaa2”, etc, the running time would be Ω(nlogn|A1|)=Ω(Llogn)\Omega(n\log n|A_{1}|)=\Omega(L\log n).

4 The string rearrangement inequalities

We call equation (7) right below the String Rearrangement Inequalities.

Lemma 6

For non-empty words A1,,AnA_{1},\ldots,A_{n}, where R(A1)R(An)R(A_{1})\leq\ldots\leq R(A_{n}), we claim that

A1A2AnAπ1Aπ2AπnAnAn1A1,A_{1}A_{2}\ldots A_{n}\leq A_{\pi_{1}}A_{\pi_{2}}\ldots A_{\pi_{n}}\leq A_{n}A_{n-1}\ldots A_{1}, (7)

for any permutation π1,,πn\pi_{1},\ldots,\pi_{n} of {1,,n}\{1,\ldots,n\}.

In other words, if several words are to be rearranged and concatenated into a string SS, the lexicographical smallest outcome of SS occurs when the words are arranged so that their repeating strings are increasing, and the lexicographical largest outcome of SS occurs when the words are arranged so that their repeating strings are decreasing. Here, the repeating string of a word AA refers to R(A)R(A).

Example 1

Suppose there are four given words: “123”, “12”, “121”, “1212”. Notice that R(121)<R(12)=R(1212)<R(123)R(121)<R(12)=R(1212)<R(123). Applying Lemma 6, the lexicographical smallest outcome would be “121121212123”, and the lexicographical largest outcome would be “123121212121”. The reader can verify this result easily.

Remark 1

If we sort the given words using the lexicographic order instead, the outcome of the concatenation is not optimum. For example, we have “12” < “121” < “1212” <“123”, and a concatenation in this order is not the smallest outcome, and a concatenation in its reverse order is neither the largest outcome.

Proof (of Lemma 6)

Consider any concatenation Aπ1AπnA_{\pi_{1}}\ldots A_{\pi_{n}}. If A1A_{1} is not at the leftmost position, we swap it with its left neighbor AxA_{x}. Note that R(A1)R(Ax)R(A_{1})\leq R(A_{x}) by assumption. According to Lemma 3, A1AxAxA1A_{1}A_{x}\leq A_{x}A_{1}. This means that the entire string becomes smaller or remains unchanged after the swapping. Applying several such swappings, A1A_{1} will be on the leftmost position. Then, we swap A2A_{2} to the second place. So on and so forth. It follows that A1AnAπ1AπnA_{1}\ldots A_{n}\leq A_{\pi_{1}}\ldots A_{\pi_{n}}.

The other inequality in (7) can be proved symmetrically; proof omitted. ∎

Combining Theorem 3.2 with Lemma 6, we obtain

Corollary 1

Given nn words A1,,AnA_{1},\ldots,A_{n} that are to be rearranged and concatenated, the smallest and largest concatenation can be found in O(i|Ai|)O(\sum_{i}|A_{i}|) time.

Another corollary of Lemma 6 is the uniqueness of the best concatenation:

Corollary 2

Given primitive and distinct words A1,,AnA_{1},\ldots,A_{n} that are to be rearranged and concatenated, the smallest (largest, resp.) concatenation is unique.

Proof

It follows from Lemma 6 and the fact that R(A1),,R(An)R(A_{1}),\ldots,R(A_{n}) are distinct (see Proposition 1 below).∎

Proposition 1

For distinct primitive words AA and BB, we have R(A)R(B)R(A)\neq R(B).

Proof

Recall that when AA and BB are primitive and R(A)=R(B)R(A)=R(B), we can infer that A=BA=B (as proved in the second paragraph of the proof of Lemma 4). Therefore, if AA and BB are primitive and distinct, R(A)R(B)R(A)\neq R(B). ∎

5 A total order \leq_{\infty} on words

Definition 4

Given primitive words AA and BB, we state that ABA\leq_{\infty}B if R(A)R(B)R(A)\leq R(B). Notice that \leq_{\infty} is a total order on primitive words by Proposition 1. Furthermore, we extend \leq_{\infty} to the scope of finite nonempty words as follows.

For non-empty words A=SkA=S^{k} and B=TlB=T^{l}, where S,TS,T are primitive, we state that ABA\leq_{\infty}B if

(S=TS=T and |S||T||S|\leq|T|), or (STS\neq T and STS\leq_{\infty}T). (8)

The symbol \leq_{\infty} in the equation stands for the relation between primitive words.

For example, 121121212121212122121\leq_{\infty}12\leq_{\infty}1212\leq_{\infty}121212\leq_{\infty}122.

Obviously, the relation \leq_{\infty} is a total order on finite nonempty words.

The next lemma shows that within the class of Lyndon words, the order \leq_{\infty} is actually the same as the lexicographical order 𝗅𝖾𝗑\leq_{\mathsf{lex}} (denoted by \leq for short). (Note that Lyndon words are primitive, so the unextended \leq_{\infty} is enough here.)

Lemma 7

Given Lyndon words AA and BB such that ABA\leq B, we have ABA\leq_{\infty}B.

Proof

Assume that ABA\neq B; otherwise we have R(A)=R(B)R(A)=R(B) and so ABA\leq_{\infty}B.

By the assumption ABA\leq B, we know A<BA<B. Consider two cases:

1. |A||B||A|\geq|B|, or |A|<|B||A|<|B| and AA is not a prefix of BB

Combining the assumption A<BA<B with the condition of this case, we can see that the relation between AB,BAAB,BA is the same as that between A,BA,B: In comparing ABAB and BABA, the result is settled before the min{|A|,|B|}\min\{|A|,|B|\}-th character.

2. |A|<|B||A|<|B| and AA is a prefix of BB, i.e., AA is a proper prefix of BB

Assume that B=ACB=AC where CC is nonempty. Because BB is a Lyndon word by assumption, AC<CAAC<CA. Therefore, AB=AAC<ACA=BAAB=AAC<ACA=BA.

In both cases, we obtain AB<BAAB<BA. It further implies that R(A)<R(B)R(A)<R(B) by Lemma 3. This means ABA\leq_{\infty}B. ∎

In fact, it is possible to further extend \leq_{\infty} to all (finite and infinite) words. Define the repeating string of an infinite word AA, denoted by R(A)R(A), to be AA itself. We state that ABA\leq_{\infty}B if R(A)<R(B)R(A)<R(B) or R(A)=R(B)R(A)=R(B) and |A||B||A|\leq|B|.

6 Conclusions

In this paper, we present a simple proof of the “string rearrangement inequalities” (7). These inequalities have not been reported in literature to the best of our knowledge. We also study the algorithmic aspect of these two inequalities, and present a linear time algorithm for rearranging the strings so that R(A1)R(An)R(A_{1})\leq\ldots R(A_{n}). This algorithm beats the trivial sorting algorithm by a factor of logn\log n.

The algorithm itself is direct (indeed, it looks somewhat brute-force) and easy to implement, yet the analysis of its correctness and complexity is build upon nontrivial observations, namely, Lemma 3, Lemma 4, and Lemma 5.

In the future, it is a problem worth attacking that whether we can improve the running time for sorting R(A1),,R(An)R(A_{1}),\ldots,R(A_{n}) from O(L)O(L) to O(N)O(N), where NN denotes the number of nodes in the trie of A1,,AnA_{1},\ldots,A_{n}.

The order \leq_{\infty} on primitive words has nice connections with repeating decimals as shown in the proof of Lemma 3. It would be interesting to know whether these connections have more applications in the study of primitive words.

References

  • [1] Orderings - oeiswiki (April 2022), https://oeis.org/wiki/Orderings
  • [2] Wikipedia: Shortlex order (April 2022), https://en.wikipedia.org/wiki/Shortlex_order
  • [3] Alatabbi, A., Daykin, J., Rahman, M., Smyth, W.: Simple linear comparison of strings in v-order. In: Pal, S., Sadakane, K. (eds.) WALCOM 2014, LNCS 8344. pp. 80–89 (2014)
  • [4] Au, Y.: Generalized de bruijn words for primitive words and powers. Discrete Mathematics 338(12), 2320–2331 (2015). https://doi.org/10.1016/j.disc.2015.05.025
  • [5] Bannai, H., I, T., Inenaga, S., Nakashima, Y., Takeda, M., Tsuruta, K.: The “runs” theorem. SIAM Journal on Computing 46(5), 1501–1514 (2017). https://doi.org/10.1137/15M1011032
  • [6] Berstel, J., Boasson, L.: The set of lyndon words is not context-free. Bull. EATCS 63 (1997)
  • [7] Chen, K., Fox, R., Lyndon, R.: Free differential calculus, iv. the quotient groups of the lower central series. Annals of Mathematics pp. 81–95 (1958)
  • [8] Crochemore, M., Russo, L.: Cartesian and lyndon trees. Theoretical Computer Science 806,  1–9 (2020). https://doi.org/10.1016/j.tcs.2018.08.011
  • [9] Daykin, D., Daykin, J., Smyth, W.: String comparison and lyndon-like factorization using v-order in linear time. In: Giancarlo, R., Manzini, G. (eds.) CPM 2011, LNCS 6661. pp. 65–76 (2011)
  • [10] D.E. Knuth, J.M., Pratt, V.: Fast pattern matching in strings. SIAM Journal on Computing 6(2), 323–350 (1977). https://doi.org/10.1137/0206024
  • [11] Dolce, F., Restivo, A., Reutenauer, C.: On generalized lyndon words. ArXiv abs/1812.04515 (2019)
  • [12] Dömösi, P., Horváth, S., Ito., M.: On the connection between formal languages and primitive words. In: Proc. First Session on Scientific Communication. pp. 59–67. Univ. of Oradea, Oradea, Romania (June 1991)
  • [13] Dömösi, P., Ito, M.: Context-free languages and primitive words (November 2014). https://doi.org/10.1142/7265
  • [14] Duval, J.: Factorizing words over an ordered alphabet. Journal of Algorithms 4(4), 363–381 (1983). https://doi.org/10.1016/0196-6774(83)90017-2
  • [15] Fredricksen, H., Maiorana, J.: Necklaces of beads in kk colors and kk-ary de bruijn sequences. Discrete Math. 23, 207–210 (1979)
  • [16] Lischke, G.: Primitive words and roots of words. Acta Univ. Sapientiae, Informatica 3(1), 5–34 (2011)
  • [17] Lothaire, M.: Combinatorics on Words. Encyclopedia of Mathematics, Vol. 17, Addison-Wesley, MA (1983)
  • [18] Lyndon, R., Schützenberger, M.: The equation am=bncpa^{m}=b^{n}c^{p} in a free group. Michigan Math. J. 9(4), 289–298 (December 1962). https://doi.org/10.1307/mmj/1028998766
  • [19] Petersen, H.: On the language of primitive words. Theoretical Computer Science 161, 141–156 (1996)
  • [20] Smyth, W.: Computing regularities in strings: A survey. European Journal of Combinatorics 34(1), 3–14 (2013). https://doi.org/10.1016/j.ejc.2012.07.010
  • [21] Zhang, D., Jin, K.: Fast algorithms for computing the statistics of pattern matching. IEEE Access 9, 114965–114976 (2021). https://doi.org/10.1109/ACCESS.2021.3105607

Appendix 0.A An alternative proof of Lemma 3

Below we show an alternative proof of Lemma 3. This proof is less clever and much more involved (compared to the other proof in section 2), yet it reflects more insights which helped us in designing our linear time algorithm.

Below we always assume that AA, BB, XX, YY are words.

Definition 5

Word AA is truly less than word BB, if there exists a prefix pair A1A2AiA_{1}A_{2}...A_{i} and B1B2BiB_{1}B_{2}...B_{i}, in which A1A2Ai1A_{1}A_{2}...A_{i-1} and B1B2Bi1B_{1}B_{2}...B_{i-1} are equal and AiA_{i} is less than BiB_{i}. For convenience, let A<TBA<_{T}B denote this case for the rest of this paper. Note that ii can be 1 such that A1A_{1} is less than B1B_{1}.

For any pair of nonempty words, AA and BB, we can generalize 3 following properties with Definition 5. Note that any XX or YY in the following properties can be any word, including empty word.

Claim (1)

Proposition A<TBA<_{T}B is equivalent to AX<BYAX<BY, if A is not prefix of B and B is not prefix of A.

Proof

If A is not prefix of B and B is not prefix of A, the proposition AX<BYAX<BY implies that A and B fits the case described in Definition 5 and thus A<TBA<_{T}B holds. The proposition A<TBA<_{T}B, by Definition 5, also indicates that AX<BYAX<BY. ∎

Claim (2)

If A<TBA<_{T}B, it holds that AX<TBYAX<_{T}BY.

Proof

If A<TBA<_{T}B, by Definition 5, there exists a prefix pair A1,A2AiA_{1},A_{2}...A_{i} and B1B2BiB_{1}B_{2}...B_{i}, in which A1A2Ai1A_{1}A_{2}...A_{i-1} and B1B2Bi1B_{1}B_{2}...B_{i-1} are equal and AiA_{i} is less than BiB_{i}. Since AA is the prefix of AXAX and BB is the prefix of BYBY, AXAX and BYBY also have the prefix pair A1A2AiA_{1}A_{2}...A_{i} and B1B2BiB_{1}B_{2}...B_{i} mentioned above, thus it holds that AX<TBYAX<_{T}BY by Definition 5. ∎

Claim (3)

If A<BA<B and |A|=|B||A|=|B|, it holds that A<TBA<_{T}B.

Proof

If A<BA<B and |A|=|B||A|=|B|, we can find a substring pair A1A2AiA_{1}A_{2}...A_{i} and B1B2BiB_{1}B_{2}...B_{i}, in which A1A2Ai1A_{1}A_{2}...A_{i-1} and B1B2Bi1B_{1}B_{2}...B_{i-1} are equal and AiA_{i} is less than BiB_{i}. This is exactly the case of Definition 5, so naturally A<TBA<_{T}B. ∎

Now, we are ready for proving Lemma 3.

Recall that this lemma states for non-empty words AA and BB, the relation between R(A)R(A) and R(B)R(B) is the same as the relation between ABAB and BABA.

We will prove R(A)=R(B)AB=BAR(A)=R(B)\Leftrightarrow AB=BA and R(A)<R(B)AB<BAR(A)<R(B)\Leftrightarrow AB<BA. Note that R(A)>R(B)AB>BAR(A)>R(B)\Leftrightarrow AB>BA can be obtained similarly.

Proposition 2

For nonempty words AA and BB, AB=BAR(A)AB=BA\Leftrightarrow R(A)=R(B)R(B).

Proof

From AB=BAAB=BA or R(A)=R(B)R(A)=R(B), we obtain from Lemma 1 that A=CkA=C^{k} and B=ClB=C^{l} for some C,k,lC,k,l, which implies that R(A)=R(B)R(A)=R(B) and AB=BAAB=BA.∎

Proposition 3

For nonempty words AA and BB, AB<BAR(A)<R(B)AB<BA\Leftrightarrow R(A)<R(B).

We prove the two directions separately in the following.

Proof (of AB<BAR(A)<R(B)AB<BA\Rightarrow R(A)<R(B))

We discuss two subcases.

Subcase 1. |A||B||A|\leq|B|

Let B=AmSB=A^{m}S, in which m=degB(A)m=\deg_{B}(A) by Definition 3.

Note that target R(A)<R(B)R(A)<R(B) equals to R(A)<R(AmS)R(A)<R(A^{m}S), which then equals to R(A)<SR(AmS)R(A)<SR(A^{m}S), by eliminating the leading AmA^{m}.

With AB<BAAB<BA, we will get the relation that AB<BAAB<BA leads to Am+1S<AmSAA^{m+1}S<A^{m}SA. And Am+1S<AmSAA^{m+1}S<A^{m}SA leads to AS<SAAS<SA, which eventually leads to AS<TSAAS<_{T}SA.

We will prove that AA<TSAAA<_{T}SA. And since AAAA is a prefix of R(A)R(A) and SASA is a prefix of SR(AmS)SR(A^{m}S), proposition R(A)<SR(AmS)R(A)<SR(A^{m}S) follows by Claim 2, proving the target proposition.

Now we prove AA<TSAAA<_{T}SA in two cases.

1. If |A||S||A|\leq|S|, or |A|>|S||A|>|S| but SS is not a prefix of AA, note that AA is not a prefix of SS, since AS<SAAS<SA, proposition A<TSA<_{T}S follows by Claim 1. Then AA<TSAAA<_{T}SA follows by Claim 2.

2. If |A|>|S||A|>|S| and SS is a prefix of AA, let A=STA=ST. Since AS<TSAAS<_{T}SA, we have STS<TSSTSTS<_{T}SST, then AA=STST<TSST=SAAA=STST<_{T}SST=SA follows by Claim 2. Thus AA<TSAAA<_{T}SA.

Subcase 2. |A|>|B||A|>|B|

Let A=BmSA=B^{m}S, in which m=degA(B)m=\deg_{A}(B).

Note that target R(A)<R(B)R(A)<R(B) equals to R(BmS)<R(B)R(B^{m}S)<R(B), which then equals to SR(BmS)<R(B)SR(B^{m}S)<R(B), by eliminating the leading BmB^{m}.

With AB<BA, we will get the relation that AB<BAAB<BA leads to BmSB<Bm+1SB^{m}SB<B^{m+1}S. And BmSB<Bm+1SB^{m}SB<B^{m+1}S leads to SB<BSSB<BS, which eventually leads to SB<TBSSB<_{T}BS.

We will prove that SB<TBBSB<_{T}BB. And since BBBB is a prefix of R(B)R(B) and SBSB is a prefix of SR(BmS)SR(B^{m}S), proposition SR(BmS)<R(B)SR(B^{m}S)<R(B) follows by Claim 2, proving the target proposition.

Now we prove SB<TBBSB<_{T}BB in 2 cases.

1. If |B||S||B|\leq|S| , or |B|>|S||B|>|S| but SS is not a prefix of BB, note that BB is not a prefix of SS, since SB<BSSB<BS, proposition S<TBS<_{T}B follows by Claim 1. Then SB<TBBSB<_{T}BB follows by Claim 2.

2. If |B|>|S||B|>|S| and SS is a prefix of BB, let B=STB=ST. Since SB<TBSSB<_{T}BS, we have SST<TSTSSST<_{T}STS, then SB=SST<TSTST=BBSB=SST<_{T}STST=BB follows by Claim 2. Thus SB<TBBSB<_{T}BB.

With both cases proved, we have AB<BAR(A)<R(B)AB<BA\Rightarrow R(A)<R(B). ∎

Proof (of R(A)<R(B)AB<BAR(A)<R(B)\Rightarrow AB<BA)

In the following, we discuss two subcases.

Subcase 1. |A||B||A|\leq|B|.

Let B=AmSB=A^{m}S, in which m=degB(A)m=\deg_{B}(A).

Note that AB<BAAB<BA equals to Am+1S<AmSAA^{m+1}S<A^{m}SA, which equals to AS<SAAS<SA by eliminating the leading AmA^{m}.

With R(A)<R(B)R(A)<R(B), we will get the relation that R(A)<R(B)R(A)<R(B) equals to R(A)<R(AmS)R(A)<R(A^{m}S). And R(A)<R(AmS)R(A)<R(A^{m}S) equals to R(A)<SR(AmS)R(A)<SR(A^{m}S) by eliminating the leading AmA^{m}.

Now we will prove AS<SAAS<SA.

1. If |A|<=|S||A|<=|S|, or |A|>|S||A|>|S| and SS is not a prefix of AA, note that AA is not a prefix of SS, since R(A)<SR(AmS)R(A)<SR(A^{m}S), A<TSA<_{T}S follows by Claim 1. Then AS<SAAS<SA follows by Claim 2.

2. If |A|>|S||A|>|S| and SS is a prefix of AA, let A=STA=ST. Since R(A)<SR(AmS)R(A)<SR(A^{m}S), we have R(ST)<SR((ST)mS)R(ST)<SR((ST)^{m}S), we pay attention to the prefixes with length 2*|S|+|T| of these two infinite words: STSSTS and SSTSST. We argue that STSSSTSTS\neq SST otherwise ST=TSST=TS, then S,T,B,AS,T,B,A are powers of a common element by Lemma 1, then R(A)=R(B)R(A)=R(B), which is contradictory. Thus, since R(ST)<SR((ST)mS)R(ST)<SR((ST)^{m}S), we will have STS<SSTSTS<SST. It holds that AS=STS<SST=SAAS=STS<SST=SA. Thus, we end up with AS<SAAS<SA.

Subcase 2. |A|>|B||A|>|B|.

Let A=BmSA=B^{m}S, in which m=degA(B)m=\deg_{A}(B).

Note that AB<BAAB<BA equals to BmSB<Bm+1SB^{m}SB<B^{m+1}S, which equals to SB<BSSB<BS by eliminating the leading BmB^{m}.

With R(A)<R(B)R(A)<R(B), we will get the relation that R(A)<R(B)R(A)<R(B) equals to R(BmS)<R(B)R(B^{m}S)<R(B). And R(BmS)<R(B)R(B^{m}S)<R(B) equals to SR(BmS)<R(B)SR(B^{m}S)<R(B) by eliminating the leading BmB^{m}.

Now we will prove SB<BSSB<BS, in two cases.

1. If |B|<=|S||B|<=|S|, or |B|>|S||B|>|S| and SS is not a prefix of BB, note that BB is not a prefix of SS, since SR(BmS)<R(B)SR(B^{m}S)<R(B), S<TBS<_{T}B follows by Claim 1. Then SB<BSSB<BS follows by Claim 2.

2. If |B|>|S||B|>|S| and SS is a prefix of BB, let B=STB=ST. Since SR(BmS)<R(B)SR(B^{m}S)<R(B), we have SR((ST)mS)<R(ST)SR((ST)^{m}S)<R(ST). we pay attention to the prefixes with length 2|S|+|T|2*|S|+|T| of these two infinite words: SSTSST and STSSTS. We can argue that SSTSTSSST\neq STS otherwise ST=TSST=TS, then S,T,B,AS,T,B,A are powers of a common element by Lemma 1, then R(A)=R(B)R(A)=R(B), which is contradictory. Thus, since SR((ST)mS)<R(ST)SR((ST)^{m}S)<R(ST), we will have SST<STSSST<STS. It holds that SB=SST<STS=BSSB=SST<STS=BS. Thus, we end up with SB<BSSB<BS.

With both cases proved, we have R(A)<R(B)AB<BAR(A)<R(B)\Rightarrow AB<BA.∎

Now, with both subcases proved, we have R(A)<R(B)AB<BAR(A)<R(B)\Leftrightarrow AB<BA.