Find the number of elements in $\{0,1\}^n$ with no more than three $1$'s or three $0$'s in a row I'm trying to find a general formula for the number of elements $s_n$ in $\{0,1\}^n$ with no more than three $1$'s or three $0$'s in a row, where $n\geq1$.
I calculated $s_n$ for small values of $n$ but could not really come up with a formula to prove by induction.
I also approached the problem combinatorially by considering two groups, one of three $0$'s and one of three $1$'s, and arranging them together with other arbitrary elements totalling $n$ elements in all, but since we want to find the number of elements with no more than three $0$'s or $1$'s in a row, and not exactly three, this approach does not work either
 A: A step-by-step sketch of an approach for a version of the problem (where you're avoiding three consecutive zeros or ones, not four - I misread the problem).  This can be entirely generalized to the case under consideration.   (Details are needed to be filled in):

*

*Let $s_n$ be the number of elements in $\{0,1\}^n$ with no triples of $0$'s or $1$'s.


*Let $s_n^0$ be the number of elements in $\{0,1\}^n$ starting with $0$ and no triples of $0$'s or $1$'s.


*Similarly, define $s_n^1$ to be the number of elements in $\{0,1\}^n$ starting with $1$ and no triples of $0$'s or $1$'s.


*Observe that $s_n=s_n^0+s_n^1$.


*By exchanging $0$ and $1$, $s_n^0=s_n^1$.


*Observe that there is a recurrence $s_n^0=s_{n-1}^1+s_{n-2}^1$ since a sequence that starts with $0$ has either a $1$ in the second or third position (but no more).


*Observe that $s_1^1=1$ and $s_2^1=2$.


*Since $s_n^0=s_n^1$, we have that $s_n^1=s_{n-1}^1+s_{n-2}^1$.


*Multiplying this through by $2$, we have that $2s_n^1=2s_{n-1}^1+2s_{n-2}^1$ or that $s_n=s_{n-1}+s_{n-2}$.


*Moreover, $s_1=s_1^0+s_1^0=2$ and $s_2=s_2^0+s_2^1=4$.


*This is a Fibonacci sequence with different starting values, so its characteristic polynomial is $t^2-t-1$, whose roots are $\frac{1\pm\sqrt{5}}{2}$.


*The general form of such a second order recurrence relation is
$$
s_n=A\left(\frac{1+\sqrt{5}}{2}\right)^n+B\left(\frac{1-\sqrt{5}}{2}\right)^n.
$$


*Plugging in the known values for $s_1$ and $s_2$, it follows that
\begin{align*}
2&=A\left(\frac{1+\sqrt{5}}{2}\right)+B\left(\frac{1-\sqrt{5}}{2}\right)\\
4&=A\left(\frac{3+\sqrt{5}}{2}\right)+B\left(\frac{3-\sqrt{5}}{2}\right).
\end{align*}


*Now, you can solve for $A$ and $B$ using linear algebra to get an explicit form for the recurrence.

 Subtracting the first equation from the second yields $2=A+B$.  Further solving gives that $A=1+\frac{1}{\sqrt{5}}$ and $B=1-\frac{1}{\sqrt{5}}$.  You can check that this works for the first few values of $n$ and prove it in general using the recurrence relation.

A: This question can be solved in two ways such as using analytical combinatorics techniques and using linear algebra. Here, i will use analytical combinatorics techniques.My method is called Goulden-Jackson Cluster Method.
To be able to understand my answer , you must firstly read the article in given link.
If more than three consecutive ones and zeros are prohibited , it is understood that we cannot have $0000,1111,00000,11111,000000,111111,...$.In other words , we cannot have four consecutive ones or zeros in any string , because all of the banned words contain four consecutive zeros or ones.
Then our bad words are $\{0000,1111\}$ in our string.
Now , it is clear that our alphabet is $V=\{0,1\}$
According to the article in page $7$ , the generating function for the string is $$A(x)=\frac{1}{1-dx-weight(C)}$$ where $d=|V|=2$ and $\text{weight(C)=weight(C[0000])+weight(C[1111])}$
You can find how to calculate $\text{weight(C[0000]) and weight(C[1111])}$ in given article , so i am not getting in it.

*

*$\text{weight(C[0000])}=-x^4-(x+x^2+x^3)\text{weight(C[0000])}$


*$\text{weight(C[1111])}=-x^4-(x+x^2+x^3)\text{weight(C[1111])}$


*$$\text{weight(C)}=\text{weight(C[0000])}+\text{weight(C[1111])}=\frac{-2x^4}{1+x+x^2+x^3}$$
Then , $$A(x)=\frac{1}{1-dx-weight(C)}=\frac{1}{1-2x+(\frac{2x^4}{1+x+x^2+x^3})}$$
$$A(x)=\frac{1+x+x^2+x^3}{1-x-x^2-x^3}$$
$$=1+2x+4x^2+8x^3+14x^4+26x^5+48x^6+...$$
That expansion means that the number of strings do not contain more than three ones and zeros consiting of $\{0,1\}$ and length $5$ is $26$.Here is the full expansion
To convert this generating function into closed form , we use enumerative combinatorics technique such that

*

*If $$A(x)=\sum_{n=0}^{\infty}a_nx^n=\frac{P(x)}{Q(x)}$$ then $$Q(x)=1+\alpha_1x+\alpha_2x^2+\alpha_3x^3+\alpha_4x^4+...+\alpha_qx^q$$ and $$a_{q+n}+\alpha_1a_{q+n-1}+\alpha_2a_{q+n-2}+\alpha_3a_{q+n-3}+...\alpha_qa_{q}=0,n \geq0$$
As a result :
$$a_n=a_{n-1}+a_{n-2}+a_{n-3}, n \geq3 ,a_1=2,a_2=4,a_3=8$$
A: While both user955791's and Michael Burr's solutions are fairly straightforward, here is another.
Consider the Regex for a valid string:
$$1\{0,3\}(01|011|001|0111|0011|0001|00111|00011|000111)^*0\{0,3\}$$
We have sixteen possible combinations of beginnings and endings for our strings with the middle being able to be determined based on solutions to a Diophantine equation.
Let $w_1,\ldots,w_9$ represent the number of $(01),\ldots,(000111)$ respectively in the final string. Then to create any valid string, we can find a multiset where the $w_i$'s are the multiplicity numbers. For beginnings and endings, the total number of digits in the beginnings and endings allows us to group them together.
Let's define some useful notation. For a given choice of $w_i$'s, define $w = \vec{w} = (w_1,\ldots, w_9)$. Then, define the following useful sums and products:
$$D(w) = 2w_1+3(w_2+w_3)+4(w_4+w_5+w_6)+5(w_7+w_8)+6w_9 \\ S(w) = \sum_{i=1}^9 w_i \\ P(w) = \prod_{i=1}^9 (w_i)! \\ f(k) = \begin{cases}1, & k\in \{0,6\} \\ 2, & k\in \{1,5\} \\ 3, & k\in \{2,4\} \\ 4, & k = 3\end{cases}$$
Now, we can write an expression for the total number of valid strings:
$$\sum_{k \in \{0,1,2,3,4,5,6\} \\ \forall i, w_i \in \mathbb{Z}_{\ge 0} \\ D(w) = n-k}f(k)\dfrac{[S(w)]!}{P(w)}$$
Both user955791's and Michael Burr's solutions are probably easier to calculate. But, this should generate the exact same results!
Update:
This is actually easier to calculate than I expected. I wrote a VBA procedure to output to Excel. It calculated the first twenty numbers in a few milliseconds.
