# What is the recurrence formula for K_n? K_n is the number of strings (a_1,a_2,...,a_n) with words from set {0, 1, 2} under the following conditions.

## This problem is originally asked by @smkwd. https://socratic.org/questions/how-to-prove-that484241 I divided it into smaller parts so as to avoid getting the answer too long. --Here is the divided question-- Consider a string (${a}_{1} , {a}_{2} , \ldots , {a}_{n}$) with words from set {0, 1, 2}. We will call the pod of the character (${a}_{i} , {a}_{i + 1} , . . . {a}_{j}$), where 1≤i≤j≤n and ${a}_{i} = {a}_{i + 1} = . . . = {a}_{j}$. The block is called maximum if it is not contained in no longer block. For example, in string (1, 0, 0, 2, 1, 1) the maximum blocks are (1), (0, 0), (2), (1, 1). Let ${K}_{n}$ be the number of such lengths $n$ of words from the set {0, 1, 2} in which all maximal blocks have odd lengths. What is the recurrence formula ${K}_{n}$ satisfies?

Oct 4, 2017

#### Answer:

${K}_{1} = 3 , {K}_{2} = 6 , {K}_{n} = 2 {K}_{n - 1} + {K}_{n - 2} \text{ } \left(n \ge 3\right)$

#### Explanation:

First we have to find ${K}_{1}$ and ${K}_{2}$.

${K}_{1} = 3$ as there are only three string: $\left(0\right) \left(1\right) \left(2\right)$.

${K}_{2} = 6$ because the 2-words string from the set $\left\{0 , 1 , 2\right\}$ in which the two maximum blocks have single word are
$\left(0 , 1\right) \left(0 , 2\right) \left(1 , 0\right) \left(1 , 2\right) \left(2 , 0\right) \left(2 , 1\right)$.

Then, what do we need to calculate ${K}_{n}$ $\left(n \ge 3\right)$ ?

Three words strings to have every maximum blocks odd words are categorized into two types.
Strings like $\textcolor{red}{\text{(0,0,0)}}$ are obtained by adding two words that are same as the last word of one word string(i.e. $\textcolor{red}{\text{(0)+(0,0)}}$ and so on.)

Strings like $\textcolor{b l u e}{\text{(0,1,2)}}$ are obtained by adding a word different from the last word of two words strings. (i.e. $\textcolor{b l u e}{\text{(0,1)+(2)}}$ and so on.)

This pattern continues for every $n \ge 3$.
The recurrence formula ${K}_{n}$ satisfies is:
${K}_{1} = 3 , {K}_{2} = 6 , {K}_{n} = \textcolor{b l u e}{2 {K}_{n - 1}} + \textcolor{red}{{K}_{n - 2}} \text{ } \left(n \ge 3\right)$