Counting binary strings of length $n$ that contain no two adjacent blocks of 1s of the same length

binarycombinatoricscombinatorics-on-words

Is it possible to count exactly the number of binary strings of length $n$ that contain no two adjacent blocks of 1s of the same length? More precisely, if we represent the string as $0^{x_1}1^{y_1}0^{x_2}1^{y_2}\cdots 0^{x_{k-1}}1^{y_{k-1}}0^{x_k}$ where all $x_i,y_i \geq 1$ (except perhaps $x_1$ and $x_k$ which might be zero if the string starts or ends with a block of 1's), we should count a string as valid if $y_i\neq y_{i+1}$ for every $1\leq i \leq k-2$.

Positive examples : 1101011 (block sizes are 2-1-2), 00011001011 (block sizes are 2-1-2), 1001100011101 (block sizes are 1-2-3-1)

Negative examples : 1100011 (block sizes are 2-2), 0001010011 (block sizes are 1-1-2), 1101011011 (block sizes are 2-1-2-2)

The sequence for the first $16$ integers $n$ is: 2, 4, 7, 13, 24, 45, 83, 154, 285, 528, 979, 1815, 3364, 6235, 11555, 21414. For $n=3$, only the string 101 is invalid, whereas for $n=4$, the invalid strings are 1010, 0101 and 1001.

Best Answer

I confirm your results for $n \le 16$. It might be useful to compute the values by conditioning on $k\in\{1,\dots,\lfloor(n+3)/2\rfloor\}$: \begin{matrix} n\backslash k & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline 0 & 1 \\ 1 & 1 & 1 \\ 2 & 1 & 3 \\ 3 & 1 & 6 & 0 \\ 4 & 1 & 10 & 2 \\ 5 & 1 & 15 & 8 & 0 \\ 6 & 1 & 21 & 22 & 1 \\ 7 & 1 & 28 & 48 & 6 & 0 \\ 8 & 1 & 36 & 92 & 25 & 0 \\ 9 & 1 & 45 & 160 & 77 & 2 & 0 \\ 10 & 1 & 55 & 260 & 196 & 16 & 0 \\ 11 & 1 & 66 & 400 & 437 & 74 & 1 & 0 \\ 12 & 1 & 78 & 590 & 883 & 254 & 9 & 0 \\ 13 & 1 & 91 & 840 & 1652 & 726 & 54 & 0 & 0 \\ 14 & 1 & 105 & 1162 & 2908 & 1818 & 239 & 2 & 0 \\ 15 & 1 & 120 & 1568 & 4869 & 4116 & 857 & 24 & 0 & 0 \\ 16 & 1 & 136 & 2072 & 7819 & 8602 & 2627 & 156 & 1 & 0 \\ \end{matrix}

Maybe try inclusion-exclusion together with stars-and-bars? For fixed $k$, the first term of inclusion-exclusion is the number of nonnegative integer solutions to $$\sum_{j=1}^k x_j + \sum_{j=1}^{k-1} y_j = n - (k-2) - (k-1) = n-2k+3,$$ which is $$\binom{(n-2k+3) + (2k-1)-1}{(2k-1)-1} = \binom{n+1}{2k-2}.$$ For $k\in\{1,2\}$, this formula is correct. For $k \ge 3$, it is only an upper bound.


An alternative approach is to condition on the tail $(y_{k-1},x_k)$. Explicitly, let state space $$S_n = \left\{k \in \{1,\dots,\lfloor(n+3)/2\rfloor\}, y \in \{[k\not=1],\dots,n\}, x \in \{0,\dots,n-y-2k+5\}\right\}.$$ For $(k,y,x) \in S_n$, let $f_n(k,y,x)$ be the number of such binary strings that end in $1^y 0^x$. Then $f$ satisfies the recursion $$f_n(k,y,x) = \begin{cases} 1 &\text{if $n = 0$} \\ [y = 0 \land x = n] &\text{if $k = 1$} \\ \sum\limits_{\substack(k-1,y_{k-2},x_{k-1}) \in S_{n-y-x}:\\ y_{k-2} \not= y \land ((y_{k-2} \ge 1 \land x_{k-1} \ge 1) \lor k=2)} f_{n-y-x}(k-1,y_{k-2},x_{k-1}) &\text{otherwise} \end{cases}$$

The desired values are then $\sum\limits_{(k,y,x) \in S_n} f_n(k,y,x)$.