A square-free word is a word consisting of arbitrary symbols where the pattern \$XX\$ (for an arbitrary non-empty word \$X\$) does not appear. This pattern is termed a "square". For example, squarefree
is not square-free (using the lowercase letters as the alphabet), as the square ee
appears; but word
is square-free. Additionally, note that \$X\$ does not have to be a single symbol, so 1212
is not square-free, as 12
is repeated.
If the alphabet used has exactly one symbol, there are only two square-free words: the empty word, and the word of exactly one symbol.
For a binary alphabet, e.g. \$\{0,1\}\$, there are a finite number of square-free words: the empty word, \$0, 1, 01, 10, 010, 101\$. All other words made from just two symbols will contain a square.
However, for an alphabet of three or more symbols, there are an infinite number of square-free words. Instead, we can consider the number of words of length \$n\$ for an alphabet of \$k\$ characters:
\$\downarrow\$ Alphabet length \$k\$ Word length \$n\$ \$\rightarrow\$ |
\$0\$ | \$1\$ | \$2\$ | \$3\$ | \$4\$ | \$5\$ | \$6\$ |
---|---|---|---|---|---|---|---|
\$1\$ | \$1\$ | \$1\$ | \$0\$ | \$0\$ | \$0\$ | \$0\$ | \$0\$ |
\$2\$ | \$1\$ | \$2\$ | \$2\$ | \$2\$ | \$0\$ | \$0\$ | \$0\$ |
\$3\$ | \$1\$ | \$3\$ | \$6\$ | \$12\$ | \$18\$ | \$30\$ | \$42\$ |
\$4\$ | \$1\$ | \$4\$ | \$12\$ | \$36\$ | \$96\$ | \$264\$ | \$696\$ |
\$5\$ | \$1\$ | \$5\$ | \$20\$ | \$80\$ | \$300\$ | \$1140\$ | \$4260\$ |
\$6\$ | \$1\$ | \$6\$ | \$30\$ | \$150\$ | \$720\$ | \$3480\$ | \$16680\$ |
For example, there are \$36\$ different squarefree words of length \$3\$ using a alphabet of \$4\$ symbols:
121 123 124 131 132 134 141 142 143 212 213 214 231 232 234 241 242 243 312 313 314 321 323 324 341 342 343 412 413 414 421 423 424 431 432 434
For a ternary alphabet, the lengths are given by A006156. Note that we include the zero word lengths for \$k = 1, 2\$ in the table above.
This is a (mostly) standard sequence challenge. You must take one input \$k\$, representing the length of the alphabet. Alternatively, you may accept a list (or similar) of \$k\$ distinct symbols (e.g. single characters, the integers \$1, 2, ..., k\$, etc.). You can then choose to do one of the following:
- Take a non-negative integer \$n\$, and output the number of square-free words of length \$n\$, using an alphabet with \$k\$ symbols
- Take a positive integer \$n\$ and output the first \$n\$ elements of the sequence, where the \$i\$th element is the number of of square free words of length \$i\$ using an alphabet of \$k\$ symbols
- Note that, as \$i = 0\$ should be included, \$n\$ is "offset" by 1 (so \$n = 3\$ means you should output the results for \$i = 0, 1, 2\$)
- Take only \$k\$ as an input, and output indefinitely the number of square free words of increasing length, starting at \$i = 0\$, using the alphabet of \$k\$ symbols.
- For \$k = 1, 2\$ you may decide whether to halt after outputting all non-zero terms, or to output
0
indefinitely afterwards
- For \$k = 1, 2\$ you may decide whether to halt after outputting all non-zero terms, or to output
This is code-golf, so the shortest code in bytes wins.
Test cases
Aka, what my sample program can complete on TIO
k => first few n
1 => 1, 1, 0, 0, 0, 0, 0, 0, 0, ...
2 => 1, 2, 2, 2, 0, 0, 0, 0, 0, ...
3 => 1, 3, 6, 12, 18, 30, 42, 60, 78, ...
4 => 1, 4, 12, 36, 96, 264, 696, 1848, 4848, ...
5 => 1, 5, 20, 80, 300, 1140, 4260, 15960, 59580, ...
6 => 1, 6, 30, 150, 720, 3480, 16680, 80040, ...
7 => 1, 7, 42, 252, 1470, 8610, 50190, ...
8 => 1, 8, 56, 392, 2688, 18480, 126672, ...
9 => 1, 9, 72, 576, 4536, 35784, 281736, ...
10 => 1, 10, 90, 810, 7200, 64080, ...
11 => 1, 11, 110, 1100, 10890, 107910, ...
12 => 1, 12, 132, 1452, 15840, 172920, ...
13 => 1, 13, 156, 1872, 22308, 265980, ...
14 => 1, 14, 182, 2366, 30576, 395304, ...
15 => 1, 15, 210, 2940, 40950, 570570, ...