[Math] Find the number of PINs that contain at least one sequence of three/two consecutive digits

combinationscombinatoricsdiscrete mathematics

a PIN is a string of four decimal digits, e.g. 2357, 0944 etc.

I am wondering how to find the number of PINs that contain at least one sequence of three consecutive digits $n; n+1; n+2$ (e.g. 2340, 5678 etc).

And the number of PINs that contain at least one sequence of two
consecutive digits
$n; n+1$ (e.g. 7340, 5671 etc).

It seems that I need to use inclusion and exclusion principle to do this.

Best Answer

You are right that one way to solve this problem is with the principle of inclusion and exclusion.

For the "at least 3 consecutive digits" case, note that there are 8 possible sets of 3 consecutive digits. These digits can be at the right of the pin (like X234) or at the left (234X), where X can be any digit. This gives $8 \cdot 2 \cdot 10 = 160$ pins. However, we have now counted pins with 4 consecutive digits 2 times: for example, 1234 is counted as 123X and X234. Thus we subtract the number of these pins which is 7 to get $153$ pins with at least 3 consecutive digits.

You can extend this method to the second part too. Add all pins with 2 known consecutive digits, subtract some with 3 known consecutive digits, add some with 4 consecutive digits, where "some" is chosen such that each of those categories is counted exactly once.