TITLE: Words built from $\{0,1,2\}$ with restrictions which are not so easy to accomodate.
QUESTION [14 upvotes]: We assume a ternary alphabet $V=\{0,1,2\}$ and are looking for a generating function describing the number of words of $V^*$ fulfilling certain restrictions. The words I am interested in  do not contain runs of length $k+1$ (with $k\geq 1$) and  do contain the string $1^k2$, i.e. a run of $1$ of length $k$ followed by $2$.

I'm aware of two techniques which are useful for attacking questions of this kind.
Smirnow words: One of them is based upon Smirnov words, which are words with no consecutive equal letters. See e.g. example III.24 in  Analytic Combinatorics by P. Flajolet and R. Sedgewick. The Smirnov words of the three letter alphabet $V$ are represented by the generating function $S(z)$
  \begin{align*}
S(z)=\left(1-\frac{3z}{1+z}\right)^{-1}
\end{align*}
  Since we are looking for words having maximal runs of $0,1$ and $2$ of length $k$ we substitute
  \begin{align*}
z\rightarrow z+z^2+\cdots+z^k=z\frac{1-z^k}{1-z}
\end{align*}
  Words which do not contain runs of length $k+1$ can therefore obtained as
  \begin{align*}
\left(1-\frac{3z\frac{1-z^k}{1-z}}{1+z\frac{1-z^k}{1-z}}\right)^{-1}=\frac{1-z^{k+1}}{1-3z+2z^{k+1}}
\end{align*}

$$ $$

The Goulden-Jackson Cluster Method nicely presented by J. Noonan and D. Zeilberger is predestinated if we are looking for words which are not allowed to contain so-called bad words. Applying this technique it is easy to find a generating function $T(z)$ which do not contain the bad word $1^k2$. According to the formula in page $7$ of the referred paper we obtain
  \begin{align*}
T(z)=\frac{1}{1-3z+z^{k+1}}
\end{align*}
  The generating function $S(z)$ can also be easily obtained with this method. Again according to the formula in page $7$ we get
  \begin{align*}
S(z)=\frac{1}{1-3z+3\frac{z^{k+1}(1-z)}{1-z^{k+1}}}=\frac{1-z^{k+1}}{1-3z+2z^{k+1}}
\end{align*}

I have problems to derive a generating function which follows the combined requirements of counting words which do not contain words with runs of length $k+1$, but contain the substring $1^k2$. Any ideas?
Note: This question corresponds to the second part of this question.

REPLY [3 votes]: Note: The instructive answer from @MarkoRiedel was worth a somewhat in-depth analysis. The outcome of this analysis serves here  as  supplementary information. We consider his approach  in some  detail,  analyse the  connection with Smirnov words and  find some simplified representations.

The essence of his answer is a clever two step decomposition of the language under consideration. As small starter we look at a similar decomposition in a simpler context regarding binary words. 
Binary words 
Non-empty binary words built from an alphabet $\{0,2\}$ and starting  with the letter $0$ admit following characterization. They  decompose into blocks, each block starting with $2$ and followed by zero or more $0$s. The word  starts with one or more $0$s. Here is an example with $|$ indicating the block decomposition.
\begin{align*}
000|2|20|200|20|20|2|2000|200|2|2|2|200
\end{align*}
The language $\mathcal{B}_0$ describing these words is 
\begin{align*}
\mathcal{B}_0=00^*\left(20^*\right)^*
\end{align*}
  with star $^*$ denoting zero or more occurrences. The corresponding generating function is
  \begin{align*}
B(z)&=\frac{z}{1-z}\sum_{q=0}^{\infty}\left(\frac{z}{1-z}\right)^q\\
&=\frac{z}{1-z}\frac{1}{1-\frac{z}{1-z}}\\
&=\frac{z}{1-2z}\\
&=z+2z^2+4z^3+8z^4+16z^5+32z^6+O(z^7)
\end{align*}
  Now let's exchange the role of $0$ and $2$ to obtain the language $\mathcal{B}_2$ of words starting with $2$.
  \begin{align*}
222|0|02|022|02|02|0|0222|022|0|0|0|022
\end{align*}
The language $\mathcal{B}_2$ describing these words is 
\begin{align*}
\mathcal{B}_2=22^*\left(02^*\right)^*
\end{align*}
  and the corresponding generating function is the same as before
  \begin{align*}
B(z)=\frac{z}{1-2z}
\end{align*}
  Observe that all binary words can be described as
  \begin{align*}
\varepsilon\cup\mathcal{B}_0\cup\mathcal{B}_2\tag{1}
\end{align*}
  which comprises either the empty word or a binary word starting with $0$ or starting with $2$. The generating function is accordingly
  \begin{align*}
1+\frac{2z}{1-2z}&=\frac{1}{1-2z}\\
&=1+2z+4z^2+8z^3+16z^4+32z^5+O(z^6)
\end{align*}

We are now well prepared for the first step in the two-step approach of Marko's answer and derive $H(z)$.

Binary Smirnov words:
We again use the binary alphabet $\{0,2\}$. This time we are looking for words which have runs of length maximal $k$. We use a  similar decomposition as we did for the language $\mathcal{B}_0$ above.
We consider zero or more blocks, each block starting with $2$ and followed by at least one or more $0$s. The words start with at least one zero and end with zero or more $2$s. The overall restriction for these words is that the maximum run length is $k$. This sounds complicated, but when we look at the formal language description, let's denote it $\mathcal{H}_0$, it is not that hard.
  \begin{align*}
\mathcal{H}_0=00^{<k}\left(22^{<k}00^{<k}\right)^*2^{<{k+1}}\tag{2}
\end{align*}
The pattern $00^{<k}$ denotes at least one $0$ followed by zero up to $k-1$ zeros. This way we create non-empty strings of $0$ with run length $\leq k$.
The pattern $\left(22^{<k}00^{<k}\right)^*$ denotes the block decomposition.
The corresponding generating function $H(z)$ for the language $\mathcal{H}_0$ can be directly translated from (2)
  \begin{align*}
H(z)&=z\frac{1-z^k}{1-z}\sum_{q=0}^{\infty}\left(z\frac{1-z^k}{1-z}\right)^{2q}\frac{1-z^{k+1}}{1-z}\\
&=z\frac{1-z^k}{1-z}\cdot\frac{1}{1-\left(z\frac{1-z^k}{1-z}\right)^2}\cdot\frac{1-z^{k+1}}{1-z}\\
&=z\frac{1-z^k-z^{k+1}+z^{2k+1}}{1-2z+2z^{k+2}-z^{2k+2}}\tag{3}\\
&=z\frac{1-z^k}{1-2z+z^{k+1}}
\end{align*}

Note that the representation (3) is stated as $H(z)$ in Marko's answer. It can be further simplified  since numerator and denominator both contain the factor $1-z^{k+1}$.

As we did in the first section, we exchange the roles of $0$ and $2$, define the language
  \begin{align*}
\mathcal{H}_2=22^{<k}\left(00^{<k}22^{<k}\right)^*0^{<{k+1}}
\end{align*}
  with the same corresponding generating series $H(z)$
  \begin{align*}
H(z)=z\frac{1-z^k}{1-2z+z^{k+1}}
\end{align*}
If we consider the language consisting of the empty word together with $\mathcal{H}_0$ and $\mathcal{H}_2$
  \begin{align*}
\varepsilon\cup\mathcal{H}_0\cup\mathcal{H}_2\tag{4}
\end{align*}
  we obtain all binary words having run length $\leq k$. Indeed, the generating function is
  \begin{align*}
1+2H(z)&=1+2z\frac{1-z^k}{1-2z+z^{k+1}}\\
&=\frac{1-z^{k+1}}{1-2z+z^{k+1}}
\end{align*}
The binary Smirnov words $B_2(z)$ are the words over a binary alphabet without consecutive equal letters, i.e. having runs of maximum length $1$. Their generating function is specified as (see my question above)
  \begin{align*}
B_2(z)&=\left(1-\frac{2z}{1+z}\right)^{-1}=\frac{1+z}{1-z}\\
&=1+2z+2z^2+2z^3+2z^4+2z^5+O(z^6)
\end{align*}
  The generating function for all binary words having runs of length $\leq k$ is consequently
  \begin{align*}
B_2\left(z\frac{1-z^{k}}{1-z}\right)&=\frac{1+z\frac{1-z^{k}}{1-z}}{1-z\frac{1-z^{k}}{1-z}}\\
&=\frac{1-z^{k+1}}{1-2z+z^{k+1}}\\
&=1+2H(z)
\end{align*}
  It follows, that the decomposition (4) is indeed the set of all binary words having run length $\leq k$.

Let's have a short look at the binary words with maximum run lengths $k=2,3,4$.
\begin{align*}
B_2\left(z\frac{1-z^{3}}{1-z}\right)&=1+2z+4z^2+6z^3+10z^4+16z^5+O(z^6)\\
B_2\left(z\frac{1-z^{4}}{1-z}\right)&=1+2z+4z^2+8z^3+14z^4+26z^5+O(z^6)\\
B_2\left(z\frac{1-z^{5}}{1-z}\right)&=1+2z+4z^2+8z^3+16z^4+30z^5+O(z^6)\\
\end{align*}
Note that the factor $1+2H(z)$ is also part of the generating function $G(z)$ in Marko Riedels answer, which we are now going to derive. The idea is to use $1+2H(z)$, i.e. the language of all binary words with maximum run length $k$ as building blocks when generating ternary words with maximum run length $k$.

Ternary Smirnov words:
Remind we are looking for ternary words over the alphabet $\{0,1,2\}$ having maximum run length $\leq k$ and each occurrence of $1^k$ has to be followed by the letter $2$. We start by creating all ternary words having maximum run length $\leq k$. The decomposition into $\mathcal{H}_0$ and $\mathcal{H}_2$ then enables us, to select precisely the words with $1^k2$ and avoid words with $1^k0$.
We consider the following language 
  \begin{align*}
\left(\varepsilon+\mathcal{H}_0+\mathcal{H}_2\right)\left(11^{<k}\left(\mathcal{H}_0+\mathcal{H}_2\right)\right)^*1^{<k+1}\tag{5}
\end{align*}
  starting with a binary, possibly empty string built from the alphabet $\{0,2\}$, followed by zero or more blocks each block starting with at least one $1$ followed by a non-empty string built from the alphabet $\{0,2\}$. A word may end with zero or more $1$s and the run length of each letter is $\leq k$. The corresponding generating function is
  \begin{align*}
G(z)&=\left(1+2H(z)\right)\sum_{q=0}^\infty\left(z\frac{1-z^k}{1-z}2H(z)\right)^q\frac{1-z^{k+1}}{1-z}\tag{6}\\
&=\left(1+2H(z)\right)\frac{1}{1-z\frac{1-z^k}{1-z}2H(z)}\frac{1-z^{k+1}}{1-z}\\
&=\frac{1-z^{k+1}}{1-2z+z^{k+1}}\frac{1}{1-z\frac{1-z^k}{1-z}2z\frac{1-z^k}{1-2z+z^{k+1}}}\frac{1-z^{k+1}}{1-z}\\
&=\frac{1-z^{k+1}}{1-3z+2z^{k+1}}
\end{align*}

Note that the representation (6) is stated as $G(z)$ in Marko's answer with the marker $v=1$. We can again derive a connection with Smirnov words.

The ternary Smirnov words $B_3(z)$ are the words over a three letter alphabet without consecutive equal letters, i.e. having runs of maximum length $1$. Their generating function is specified as (see my question above)
  \begin{align*}
B_3(z)&=\left(1-\frac{3z}{1+z}\right)^{-1}=\frac{1+z}{1-2z}\\
&=1+3z+6z^2+12z^3+24z^4+48z^5+O(z^6)
\end{align*}
  The generating function for all ternary words having runs of length $\leq k$ is consequently
  \begin{align*}
B_3\left(z\frac{1-z^{k}}{1-z}\right)&=\frac{1+z\frac{1-z^{k}}{1-z}}{1-2z\frac{1-z^{k}}{1-z}}\\
&=\frac{1-z^{k+1}}{1-3z+2z^{k+1}}\\
&=G(z)
\end{align*}
  It follows, that the decomposition (5) is indeed the set of all ternary words having run length $\leq k$.

Let's have a short look at the ternary words with maximum run lengths $k=2,3,4$.
\begin{align*}
B_3\left(z\frac{1-z^{3}}{1-z}\right)&=1+3z+9z^2+24z^3+66z^4+180z^5+O(z^6)\\
B_3\left(z\frac{1-z^{4}}{1-z}\right)&=1+3z+9z^2+27z^3+78z^4+228z^5+O(z^6)\\
B_3\left(z\frac{1-z^{5}}{1-z}\right)&=1+3z+9z^2+27z^3+81z^4+240z^5+O(z^6)\\
\end{align*}

Final step: The words with pattern $1^k2$
The final step is to isolate the words from (5) which contain $1^k2$ and no other runs of $1$ of length $k$. Due to the clever decomposition we can easily identify the language. Starting with the language in (5) we obtain
  \begin{align*}
\left(\varepsilon\right.&\left.+\mathcal{H}_0+\mathcal{H}_2\right)\left(11^{<k}\left(\mathcal{H}_0+\mathcal{H}_2\right)\right)^*1^{<k+1}\\
&=\left(\varepsilon+\mathcal{H}_0+\mathcal{H}_2\right)\left(11^{<k-1}\left(\mathcal{H}_0+\mathcal{H}_2\right)
+1^k\left(\mathcal{H}_0+\mathcal{H}_2\right)\right)^*\left(1^{<k}+1^k\right)\tag{7}
\end{align*}
  From the representation (7) we see we have to skip the strings $1^k\mathcal{H}_0$ in the block and the final string $1^k$. We so obtain the wanted language
  \begin{align*}
\left(\varepsilon+\mathcal{H}_0+\mathcal{H}_2\right)\left(11^{<k-1}\left(\mathcal{H}_0+\mathcal{H}_2\right)
+1^k\mathcal{H}_2\right)^*1^{<k}
\end{align*}
  The corresponding generating function $A_k(z)$ is
  \begin{align*}
A_k(z)&=(1+2H(z))\sum_{q=0}^\infty\left(z\frac{1-z^{k-1}}{1-z}2H(z)+z^kH(z)\right)^q\frac{1-z^k}{1-z}\\
&=(1+2H(z))
\frac{1}{1-z\frac{1-z^{k-1}}{1-z}2H(z)+z^kH(z)}\cdot\frac{1-z^k}{1-z}\\
&=\frac{1-z^{k+1}}{1-2z+z^{k+1}}\cdot
\frac{1}{1-z\frac{1-z^{k-1}}{1-z}2z\frac{1-z^k}{1-2z+z^{k+1}}-z^kz\frac{1-z^k}{1-2z+z^{k+1}}}\cdot\frac{1-z^k}{1-z}\\
&=\frac{\left(1-z^k\right)\left(1-z^{k+1}\right)}{1-3z+2z^{k+1}+2z^{k+2}-z^{2k+1}-z^{2k+2}}
\end{align*}
This generating function is slightly different to $Q(z)$ stated by Marko. A reason for it is the different stopping condition of words, since here we also avoid the string $1^k$ at the end of words.

Finally we take a short look at the ternary words with maximum run lengths $k=2,3,4$, containing $1^k2$ but no other string with $1^k$.
\begin{align*}
A_2(x)&=\frac{(1-z^2)(1-z^3)}{1-3z+2z^3+2z^4-z^5-z^6}\\
&=1+3z+8z^2+21z^3+55z^4+145z^5+O(z^6)\\
A_3(x)&=\frac{(1-z^3)(1-z^4)}{1-3z+2z^4+2z^5-z^7-z^8}\\
&=1+3z+9z^2+26z^3+75z^4+217z^5+O(z^6)\\
A_4(x)&=\frac{(1-z^4)(1-z^5)}{1-3z+2z^5+2z^6-z^9-z^10}\\
&=1+3z+8z^2+27z^3+80z^4+237z^5+O(z^6)\\
\end{align*}
The coefficients are verified with code-snippets in R.