Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working my way through basic combinatorics questions with recurrence relation, and can't quite get my head about the right way of solving them.

For example, I have two following examples in my Uni text-book:

1) Strings formed from 0,1,2 characters, needed to calculate amount of possible strings without combinations 00 and 01.

The solution is: $$f(n)=2f(n-1) + f(n-2)$$ i.e. taking only allowed characters

2) Strings formed from A,B,C characters, needed to calculate amount of possible strings without AB combination.

The solution is: $$f(n)=3f(n-1) - f(n-2) $$i.e. taking all characters and subtracting the forbidden ones.

The idea is very clear, but what I can't understand is why in first solution only valid cases are taken, and in second case all cases taken then one invalid is subtracted.

It seems that I can solve it in reverse, i.e.:
1) $f(n)=3f(n-1) - 2f(n-1)$ //i.e. 0,1,2 minus 00 and 01 case
2) $f(n)=2f(n-1)+2f(n-2)$ //i.e. B,C + AA,AC case

But when translated to difference equations, the results are different.

Anything I'm missing here?


share|cite|improve this question
If I'm not misreading your argument, you need to argue differently. It isn't just one case of $AB$, say. If at step $n$, you end your string with $A$, there are 8 3-strings at the end of the length $n$ string that are valid; but only $5$ such 3-strings. These tie the last 3 steps of your selection together. As you say, you can add $3 \cdot f(n-1)$, then need to subtract something - those are the ones ending $BB, BC$ in the last 2 steps, which then relate back somehow to $n-2$. Or this is how it seems to me. There is something more elegant which eludes me (I assume order matters). – gnometorule Feb 1 '13 at 17:19
Typos: only 5 such 3-strings ending with $B$ in step $n$. And it's $BB, CB$, not $BB, BC$ as I write above. – gnometorule Feb 1 '13 at 17:22
up vote 2 down vote accepted

In 1), "02" is an allowable length-2 string, and $002$ is not an allowable length-3 string. Your formula $$3f(n-1)-2f(n-2)$$ is "double-subtracting" the string "$00\,02$", since it is not counted by $3f(n-1)$, but is counted by $2f(n-2)$.

The correct formulation would be to subtract all the string beginning with either "01" or "002": $$f(n) = 3f(n-1) - f(n-2) - f(n-3)$$ which you can check is equivalent to the book's answer.

In 2), "BB" is an allowable length-2 string, while "$AA\,BB$" is not. But $2f(n-2)$ is counting the latter.

There is no simple additive formulation, since there are infinitely many valid prefixes: $B, C, AC, AAC, AAAC, \ldots.$

You could, however, count string beginning with $B, C, AA,$ or $AC$, and then subtract the invalid strings beginning with $AAB$, yielding

$$f(n) = 2f(n-1) + 2f(n-2) - f(n-3).$$

share|cite|improve this answer
Thanks for the great explanation! – SyBer Feb 2 '13 at 20:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.