Find and solve a recurrence equation for the number gn of ternary strings of length n that do not contain $102$ as a substring.
I am having some trouble finding the recurrence relation for this question. My thinking is that you can set this problem into cases. If the last digit of the ternary string is $0,1$,or $2$, then there is $3g(n-1)$ possible cases of length $n-1$. Then, continue to do the same for the next digits.
Any help would be appreciated. Thanks!
Consider a ‘good’ string of length $n$. If it does not end in $10$, you can append any of the three digits to make a good string of length $n+1$. If it does end in $10$, however, you can only append a $0$ or a $1$. Thus, if $a(n)$ is the number of good strings of length $n$ that end in $10$, we must have
$$g(n+1)=3\big(g(n)-a(n)\big)+2a(n)=3g(n)-a(n)\;.$$
Of course we want to get rid of $a(n)$ in favor of some combination of values of $g$. A good string of length $n$ that ends in $10$ is simply a good string of length $n-2$ with $10$ appended, and any good string of length $n-2$ will work here: appending $10$ to a good string always creates another good string. Thus, $a(n)=\ldots\;$?