How can I solve this string problem more efficiently?

43 Views Asked by At

How many bitstrings of length 4 exists, where there can't be 3 consecutive 1's in a row and can't be 3 consecutive 0's?

I solved it by writing out, by hand, all the combinations of bitstrings of length 4 and then counting how many there were that fulfilled the aforementioned criteria.

My answer is that a total of 16 bitstrings exist of length 4. The number of strings fulfilling the aforementioned criteria are 6. So there are 6 strings which do not have 3 consecutive 1's or 3 consecutive 0's.

1

There are 1 best solutions below

0
On

The number of length 4 bitstrings with 3 consecutive 1's in a row is 3: Namely 1110 and 0111 and 1111.

Similarly the number of strings with 3 consecutive 0's in a row is also 3: Namely 0001 and 0000 and 1000.

None of these possibilities coincide. Thus, the answer is $2^4 - 6 = 10$.

If you want to generalize to $n$-length strings, you can use PIE to solve.