You can uniquely encode a binary string of length n by its first digit and then n-1 choices of whether the ith position is equal to the (i+1)st position or not. For example, 10001 is encoded as [1,(change-same-same-change)]. The string 00001 is encoded as [0,(same-same-same-change)]. This encoding is useful here. A binary string has r streaks precisely when it has r-1 changes out of n-1. So, there are 2 * (n-1 choose r-1) binary strings with r streaks, where (n-1 choose r-1) is a binomial coefficient, because we can choose the first digit to be either 0 or 1 and then any set of r-1 changes out of the n-1 possible locations will produce r streaks. For example, if n=4, there are 2 strings with 1 streak, 6 strings with 2 streaks, 6 strings with 3 streaks, and 2 strings with 4 streaks. The pattern 2-6-6-2 is twice a row of Pascal's triangle.

Huaizhong R.
06/09/25