Can you speak forever without repeating yourself?

In this post, I want to describe the thought process while solving what was, in my opinion, a nontrivial problem that looks like, but is not exactly like the math olympiad questions some of the readers might have experience in.

1. The problem

Here is a very interesting elementary-looking problem: let’s have an alphabet consisting only of letters , and . A sequence of those characters is called a word. For example, and are finite words of length 2 and 3. We can also have an infinite word, for example …

A word is called square-free if it doesn’t contain the same consecutive subsequence (a substring) twice in a row. For example, is square-free, while and are not. (The first one contains twice, while the second contains the letter twice in a row.)

(The terminology comes from the free group structure of the set of all possible words: basically, one has concatenation as a binary operation, and if there is a word coming twice in a row as in , we may as well write it as .

The problem is: Does there exist an infinite square-free word in the three-letter alphabet?

2. The decision

First of all, what is the answer to the question? Are we trying to prove that the infinite word with such properties doesn’t exist, or are we trying to prove existence?

I must confess at this point that I knew the answer when I started solving the problem: it was posed as a homework problem in a class I attended in the Prove that … form. However, there are ways of deciding which “side” of the question could be proven, reminiscent of Olympiad mathematics techniques, which can be applied here.

Let’s think of the reasons why an infinite square-free word wouldn’t exist. Could it be that there simply couldn’t be a square-free word of size, say, more than ? Although it is pretty easy to come up with some small square-free words (try it yourself!), things get trickier when going forward. For example, one may want to construct a word iteratively, starting with:







Now, we must put to avoid repetition of :





And again:

Now if we try:

We corner ourselves: there is nothing we can do. If we put , we repeat ; otherwise we repeat .

Luckily, I was solving this problem at home, with access to a computer. In the year , there is really no good reason not to try to generate as many small square-free words as I could. I used a brute-force search on an implicit binary tree of words: when we fix as the starting letter, there are two of its “children“: and , which correspond to the letters that didn’t happen. Sometimes there aren’t two options, as in : you can only add now. Readers more experienced with competitive programming should recognize how to optimize this: we can use a sort of trie if we want to store everything, or recursion if we don’t.

The program written in Python and compiled with PyPy was pretty fast and generated words of size , words of size and words of size . I noticed a bit of a pattern here, so the result of around generated words of size didn’t come as much of a surprise: the number of good words was around .

Now we can assert something like: “Of course an infinite square-free word exists! The number of words grows exponentially, and there is no way there are so many forbidden letters at a given length to stop the growth!”. But, this kind of reasoning would be a bit faulty.

The experimental evidence is an argument for the existence of arbitrarily long square-free words; there is nothing wrong about that. However, arbitrarily long doesn’t directly imply infinite! For example, there are arbitrarily long arithmetics progressions consisting solely of prime numbers, but it is an easy exercise to show that there doesn’t exist an infinite arithmetic progression of primes.

We can look at the concept of an infinite word as a sequence of finite words that “converges“ to a given word. More precisely, we need an infinite sequence of words of increasing length where each word is a prefix of the next word.

(The infinite word would be a sequence of words , , , , )

Luckily, if one makes some reasonable assumptions about the distribution of square-free words of given length, a handwavy heuristic argument should point to the right answer. The ”probability” of a branch of the binary tree not happening should be around , and if a word has more than descendants of the same length, it’s pretty unlikely its branch will ever ”die off”. Using additional assumptions about probabilities, one can assert the same about one of its descendants with, say, descendants of the same length, continuing indefinitely.

Nothing of this is a formal proof of anything. However, at this point we should be convinced enough to at least try to prove that an infinite square-free word exists.

3. The wandering

Of course, I was determined to construct the convergent sequence of finite words. Proving existence without explicitly constructing the word seemed infeasible – the LLL seemed too weak to produce an infinite word when taking care of an infinite number of forbidden subwords whose lengths decreased linearly. (In hindsight, his intuition may not have been correct. It seems that there are stronger methods that prove the existence of an infinite square-free word on 4-letter alphabets.)

How to construct larger and larger words? Induction seemed like a natural way to proceed. The most obvious thing to do is to append something to the end of the previous word every time – after all, we need to satisfy the prefix condition.

But what to append? If we restrict ourselves to adding one letter at a time, we should have an algorithm to decide which letter we should add on each step. It cannot be just “add the first letter that doesn’t create a repetition” – the example in the previous section shows the failure of that exact scenario.

Here I spent hours. Whatever I tried, it failed on some step. The aforementioned procedure fails on the eighth step, a little bit smarter thing fails on step , and a horrendous -case decision procedure I won’t describe here (because I’m ashamed of believing a good solution would come along these lines) couldn’t produce a -letter finite word.

So, the “append a fixed number of letters to the previous word” solution didn’t seem promising. But what else one can do to create new words while keeping the previous words as prefixes?

4. The idea

At each step, we are creating a larger finite square-free word from a smaller one. What I was trying to construct in the previous paragraph was a function taking words in the three-word alphabet to the same set, so that the convergent sequence of words would be , , ,

And here is what I’ve been doing wrong the whole time: I was trying to pull the function out of thin air, without thinking hard about the properties I would want the function to have!

If one translates the wanted properties of the word sequence to the newly-created language of functions, things simplify up a bit. Actually, it’s not hard to see the next two properties would be “extremely” nice to have in the desired function:

(i) for each word in our sequence, is a prefix of , and:

(ii) maintains square-freeness; in other words, if is square-free, should also be.

And here the experience in doing IMO-style functional equations is useful. We can write the first condition in the form:

and plugging in instead of into gives:

and together with readily implies:

Now, must be a “factor” of for lots of words . What is the simplest way that could happen? Of course, we should first try to impose a stronger condition: should be a factor of for all words and .

Again, making things as simple as possible, what could be the reason “divides” ?

So, why not, I tried to make the function multiplicative, and the problem started solving itself.

5. The finish

If is completely multiplicative, it’s easy to see we only need to choose the values of on the one-letter words , and . One other condition is that (without losing of generality) should be a prefix of , because multiplicativity doesn’t help at the very start of the sequence. Also, the second condition now has some strong implications:

and the versions that come from other combinations of letters.

At that moment, I wrote a Python program to find some feasible choices , and using . One of the nicest things it found was:

The proof that maintains square-freeness on the sequence ( , , , is kind of tedious. The basic idea is the extremal principle: from the smallest counterexample, we generate a smaller one, or a word that cannot be in the sequence. The technique used is remniscent of some algorithmic contest problems about strings – but that is a discussion better saved for another post.

It’s important to note that some triples that satisfy don’t work when starting from : apart from the trivial ones in which doesn’t begin with ,

does not work because the square factor appears as a substring of , where comes from , and is a substring of . No such things appear in the first triple due to the “bad” string coming only from , which cannot appear in the sequence starting from .

6. The further info

The important paper by Bean, Ehrenfeucht and McNulty generalises the proof of the square-freeness described in the previous question to obtain a strong result: gives square-free words on square-free inputs from the whole alphabet if and only if, essentially, does it on the inputs of length at most three. There are lots of unsolved problems in the interesting theory of avoiding patterns in words, and I recommend the reader to look into M. Lothaire’s Algebraic Combinatorics on Words if he or she liked the problem.