[Math] Password Combination Problem

combinations

A user must choose $ n $ characters password using:

  1. uppercase letters $ A-Z (size=26)$
  2. lowercase letters $ a-z (size=26)$
  3. digits $ 0-9 (size=10)$

Each password must contain at least an uppercase and a digit.

What should be the formula to calculate number of valid passwords of size $ n $, give $ n >= 1$ ?

I have calculated it to be:

Uppercase x Digit x combination of all 3 types = $ 26\times10\times(26\times26 \times10)^{n}$

Best Answer

The easiest way to do this is probably inclusion-exclusion. There are $62^n$ strings of length $n$ using the characters provided. There are $36^n$ such strings without an uppercase letter, and $52^n$ such strings without a lowercase letter, and $26^n$ such strings without either.

Thus, the total number of valid passwords of length $n$ is $$ 62^n - 36^n - 52^n + 26^n, $$ where you add the number of strings without either back in, because you have subtracted it twice.