I think there is an mistake in @juruen orignal code. The approximation using Tribonacci numbers is interesting but you need to considere two sequences.

```
E(n) = E(n-1) + E(n-2) + E(n-3) + E'(n-1) + E'(n-2) + E'(n-3)
```

Where `E(n)`

is the number all the sequences with n characters and `E'(n)`

is the number of all sequences with n characters without any b.

You can reach this splitting `E(n)`

in three parts: sequences starting with A, with B and C: `E(n) = A(n) + B(n) + C(n)`

. It's clear that `A(n) = E(n-1)`

. `B(n) = E'(n-1)`

. But `C(n)`

is more tricky. You need to consider sequences not starting with `ccc`

, so `C(n) = A(n-1) + A(n-2) + B(n-1) + B(n-2)`

.

Using the same approach in `E'(n)`

and combining all terms you can reach a compact solution:

```
E(n) = E(n-1) + E(n-2) + E(n-3) + E'(n-1) + E'(n-2) + E'(n-3)
```

In code:

```
def solution(n):
if n <= 0:
return 0
if n == 1:
return 3
if n == 2:
return 8
E, E1, E2, E3, Ep, Ep1, Ep2, Ep3 = 19, 8, 3, 0, 7, 4, 2, 0
for _ in range(3, n):
Ep1, Ep2, Ep3 = Ep, Ep1, Ep2
E1, E2, E3 = E, E1, E2
Ep = Ep1 + Ep2 + Ep3
E = E1 + E2 + E3 + Ep1 + Ep2 + Ep3
return E
```

I can be later simplified to @juruen's current solution (that works fine), but I find this one simpler to understand.