In how many ways can the word MATTER be arranged so that none of the letters are in their original positions?
I know I have to use the Derangement formula, but the hardest thing here for me to consider is the 2 Ts in the middle. What would you have to do here?
First, treat all letters as different. For example, replace one T with X and start with the word MATXER. We'll switch 'X' back to 'T' later.
The total number of derangements of word MATXER is !6=265. However, not all derangements are valid:
(1) The letter T must not be in the fourth position. What is the number of such derangements? Actually, the letter T could be in 5 different positions and for each position we have the equal number of derangements, which is one fifth. So we have to eliminate 265/5 = 53 derangements.
(2) The letter X must not be in the third position. By using the same reasoning as in (1) we have to eliminate another 53 derangements.
But in (1) and (2) we are eliminating some derangements twice. These derangements have T in the fourth place and X in the third place. This actually means that letters M,A,E,R are deranged in four remaining positions. So the number of derangements counted twice is !4=9
Taking all this into account it seems that the final result is: 265-53-53+9=168. But it's not! That's the number of derangments of word MATXER with an additional condition that letters T and X are not either in third or fourth position. For example, words TMAERX and XMAERT are both included in the total count of 168. But when you replace X with T, both words become the same: TMAERT.
Actually all of 168 derangments come in pairs with T and X in swapped positions. So the total number of derangements of MATTER is actually 168/2=84.
The following Java code is just a brute force proof that the above result is correct:
The code prints the following list of derangements:
BTW, my favorite way to calculate the number of derangements quickly is:
$$!n=\left[\frac{n!}{e}\right]$$