4. How many natural numbers less than 10000 have exactly three equal digits?
## First solution.

Such numbers can be three-digit and four-digit.

There are 9 three-digit numbers (these are the numbers 111, 222, 333, 444, 555, 666, 777, 888, and 999).

Four-digit numbers with three identical digits can be of the form $\overline{a a a b}, \overline{a a b a}, \overline{a b a a}$, and $\overline{b a a a}$. To determine how many numbers of the form $\overline{a a a b}$ there are, note that the digits $a$ and $b$ must be different, and $a$ cannot be 0. This means that the digit $a$ can be chosen in 9 ways (any digit except 0), and then the digit $b$ in 9 ways (any except $a$). Thus, there are $9 \cdot 9=81$ numbers of the form $\overline{a a a b}$. In the same way, we conclude that there are 81 numbers of the form $\overline{a a b a}, \overline{a b a a}$, and the same applies to numbers of the form $\overline{\text { baaa }}$, except that we first choose $b$ (different from 0), and then $a$ (different from $b$). In total, there are $4 \cdot 81=324$ four-digit numbers with three identical digits. In total, there are $324+9=333$ numbers less than 10000 with three identical digits.