10

How many ternary words in seven letters do not contain the word $121$?

My naive approach is:

  • If starts with 2 or 3, move to $T(n-1)$
  • If starts with 12, move to $T(n-3)$
  • If starts with 13, move to $T(n-2)$

But what if we start with $11$? I've seen some techniques using two recursive sequences...

8 Answers8

7

Let's do it recursively, for all lengths $n$.

We let $T_n$ be the number of "good" words of length $n$.

Let $A_n$ be the number that of good words of length $n$ that end in $12$.

Let $B_n$ be the number that of good words of length $n$ that end in $1$.

Let $C_n$ be the number that of good words of length $n$ that end in anything other than $1$ or $12$.

Of course $$T_n=A_n+B_n+ C_n$$

We note that $$A_n=B_{n-1}\quad B_n=B_{n-1}+C_{n-1}\quad C_n=2A_{n-1}+B_{n-1}+2C_{n- 1}=2T_{n-1}-B_{n-1}$$ As we clearly have $$A_1=0\quad B_1=1\quad C_1=2$$ this is easy to implement . Barring error the first few $T_n$ are $$\{3,9,26,75,217,628,1817,5257,15210,\cdots\}$$ In particular $T_7=\fbox {1817}$

Sanity check: let's compute $T_4$ by hand. The bad words are $121*$ and $*121$ hence there are $6$ bad words, so $T_4=81-6=75$ as predicted. Similarly to compute $T_5$ we eliminate $9$ words of the form $121**$, $9$ words of the type $*121*$ and $9$ of the type $**121$, noting that we have eliminated $12121$ twice. Hence $T_5=3^5-27+1=243-26=217$ as desired.

lulu
  • 70,402
2

I think these cases cover the complement set:

  • Words containing $121$ exactly once
  • Words containing $121$ twice without overlap, but not $1212121$
  • Words containing $12121$, but not $1212121$
  • Words containing $1212121$ (this one's easy)

Can you take it from here?

John
  • 26,319
  • What about anderstood's commend? Why do we need to count how many overlaps etc and not just say $4\times 3^4$? – combinarcotics Sep 23 '16 at 19:27
  • 1
    Because you'd be double- or triple-counting. Whatever counting argument you use has to account for each word once, and once only. @anderstood 's argument, if taken at face value, has this issue. One of the $3^4$ strings of form $121xxxx$ is $1213121$. And one of the $3^4$ strings of form $xxxx121$ is $1213121$, the same string! See the problem? – John Sep 23 '16 at 19:31
  • Incredibly enough I do! Thanks! – combinarcotics Sep 23 '16 at 19:39
  • Hah good! (To be fair, the overlap / non-overlap distinction isn't really necessary if you're directly counting the cases, but it may help to expose the issues should you want to develop a recursive argument.) – John Sep 23 '16 at 19:43
2

Partition strings into 4 types:

  • (A) Strings containing a 121
  • (B) Else, strings ending in 12
  • (C) Else, strings ending in 1
  • (D) All other strings

Let $M_{i, j}$ be the number of ways a string in state $i$ can transtion into state $j$ by appending a single character. Example, $M_{B, D}=2$, because xxxx12 can become a (D) type string in two ways: by appending a 2 (xxxx122) or appending a 3 (xxxx123), but not by appending a 1 because xxxx121 is an (A) type string.

Those transitions can be recorded in a matrix:

$$M = \left[ \begin{array} {r|cccc} & A & B & C & D \\ \hline A & 3 & 0 & 0 & 0 \\ B & 1 & 0 & 0 & 2 \\ C & 0 & 1 & 1 & 1 \\ D & 0 & 0 & 1 & 2 \\ \end{array} \right]$$

The number of transitions over 7 characters is $M^7$:

$$M^7 = \left[ \begin{array} {r|cccc} & A & B & C & D \\ \hline A & 2187 & 0 & 0 & 0 \\ B & 931 & 134 & 388 & 732 \\ C & 564 & 173 & 501 & 949 \\ D & 370 & 194 & 561 & 1062 \\ \end{array} \right]$$

The number of strings containing 121 is $M^7{}_{D, A} = 370$; so the number of strings not containing it is $3^7 - 370 = M^7{}_{D, B} + M^7{}_{D, C} + M^7{}_{D, D} = 194 + 561 + 1062 = 1817$.

DanielV
  • 23,556
2

To follow your own approach, let $T_n$ denote ternary words of length $n$ not containing $121$. Let $1_n$ denote ternary words of length $n$ starting with a $1$ not containing $121$. Then $$ 1_n=1_{n-1}+4T_{n-3}+1_{n-2} $$ since we have words starting with $11$ as $1_{n-1}$, $122,123,132,133$ as $4T_{n-3}$, and $131$ as $1_{n-2}$. Furthermore $$ T_n=1_n+2T_{n-1} $$ since we have words starting with $1$ as $1_n$, and $2,3$ as $2T_{n-1}$. Thus we can form the following table: $$ \begin{array}{|c|c|c|} \hline n&1_n&T_n\\ \hline 0&0&1\\ \hline 1&1&3\\ \hline 2&3&9\\ \hline 3&8&26\\ \hline 4&23&75\\ \hline 5&67&217\\ \hline 6&194&628\\ \hline 7&561&1817\\ \hline \end{array} $$ which shows the answer to be $T_7=1817$.

Note that the first values for $n=0,1,2$ are seeds that one has to derive manually before the recursion gets going.

String
  • 18,395
2

The DFA method including a Maple implementation was presented at the following MSE link. In the present case we obtain the following session.

> GFNC([[1,2,1]], 3, true);
                                [[1, 2, 1]]

                                Q[], 0, Q[]

                                Q[], 1, Q[1]

                                Q[], 2, Q[]

                                Q[1], 0, Q[]

                               Q[1], 1, Q[1]

                              Q[1], 2, Q[1, 2]

                              Q[1, 2], 0, Q[]

                           Q[1, 2], 1, Q[1, 2, 1]

                              Q[1, 2], 2, Q[]

                         Q[1, 2, 1], 0, Q[1, 2, 1]

                         Q[1, 2, 1], 1, Q[1, 2, 1]

                         Q[1, 2, 1], 2, Q[1, 2, 1]

                                    2
                                   z  + 1
                           - -------------------
                                3    2
                             2 z  - z  + 3 z - 1

> series(%, z=0, 8);
                  2       3       4        5        6         7      8
     1 + 3 z + 9 z  + 26 z  + 75 z  + 217 z  + 628 z  + 1817 z  + O(z )

This confirms the answer being $1817.$

Marko Riedel
  • 61,317
2

This answer is based upon the Goulden-Jackson Cluster Method.

We consider the set words of length $n\geq 0$ built from an alphabet $$\mathcal{V}=\{0,1,2\}$$ and the set $B=\{121\}$ of bad words, which are not allowed to be part of the words we are looking for. We derive a generating function $f(s)$ with the coefficient of $s^n$ being the number of searched words of length $n$.

According to the paper (p.7) the generating function $f(s)$ is \begin{align*} f(s)=\frac{1}{1-ds-\text{weight}(\mathcal{C})}\tag{1} \end{align*} with $d=|\mathcal{V}|=3$, the size of the alphabet and $\mathcal{C}$ the weight-numerator of bad words with \begin{align*} \text{weight}(\mathcal{C})=\text{weight}(\mathcal{C}[121]) \end{align*} We calculate according to the paper \begin{align*} \text{weight}(\mathcal{C}[121])&=-s^3-s^2\text{weight}(\mathcal{C}[121])\\ \end{align*}

and get \begin{align*} \text{weight}(\mathcal{C})=-\frac{s^3}{1+s^2} \end{align*}

It follows

\begin{align*} f(s)&=\frac{1}{1-ds-\text{weight}(\mathcal{C})}\\ &=\frac{1}{1-3s+\frac{s^3}{1+s^2}}\\ &=\frac{1+s^2}{1-3s+s^2-2s^3}\\ &=1+3s+9s^2+26s^3+\color{blue}{75}s^4+217s^5+628s^6\\ &\qquad+\color{red}{1817}s^7+5257s^8+15210s^9+44007s^{10}+\cdots \end{align*}

The last line was calculated with the help of Wolfram Alpha. We see the number of valid words from the alphabet $\{0,1,2\}$ of length $7$ which do not contain the word $121$ is $\color{red}{1817}$.

So, e.g. out of $3^4=81$ ternary words of length $4$ there are $\color{blue}{75}$ valid words and $6$ invalid words marked blue in the table below.

\begin{array}{ccccccccc} 0000&0100&0200&1000&1100&1200&2000&2100&2200\\ 0001&0101&0201&1001&1101&1201&2001&2101&2201\\ 0002&0102&0202&1002&1102&1202&2002&2102&2202\\ 0010&0110&0210&1010&1110&\color{blue}{1210}&2010&2110&2210\\ 0011&0111&0211&1011&1111&\color{blue}{1211}&2011&2111&2211\\ 0012&0112&0212&1012&1112&\color{blue}{1212}&2012&2112&2212\\ 0020&0120&0220&1020&1120&1220&2020&2120&2220\\ 0021&\color{blue}{0121}&0221&1021&\color{blue}{1121}&1221&2021&\color{blue}{2121}&2221\\ 0022&0122&0222&1022&1122&1222&2022&2122&2222\\ \end{array}

Markus Scheuer
  • 108,315
1

The coupled recurrence approach you mention goes like this: Let $T(n)$ be the number of good strings of length $n$. Let $U(n)$ be the number of good strings that start with $21$. Let $V(n)$ be the number of good strings that start with $1$. Let $W(n)$ be the number of good strings that start other than $1$ or $21$. Then $$T(n)=U(n)+V(n)+W(n)\\ U(n)=V(n-1)\\V(n)=T(n-1)-U(n-1)=V(n-1)+W(n-1)\\W(n)=2U(n-1)+2V(n-1)+2W(n-1)$$ You can imagine a column vector of $(U(n),V(n),W(n))^T$ and find the matrix that takes you from $n-1$ to $n$. Diagonalize the matrix and the eigenvalues are the rates of growth of each eigenvector.

Ross Millikan
  • 374,822
1

For the $n=7$ case, here's an inclusion-exclusion approach that counts the number of words that avoid the five properties that $121$ appears starting in position $1$, $2$, $3$, $4$, or $5$: $$\binom{5}{0}3^7 - \binom{5}{1}3^{7-3} + \left[3 \cdot 3^{7-6} + 3 \cdot 3^{7-5}\right] - 1 \cdot 3^{7-7} = 2187-405+36-1=1817$$ The two terms in square brackets for the two-property case arise from whether the two $121$ blocks are disjoint or intersect. The final term corresponds to the only three-property case: $1212121$.

RobPratt
  • 45,619