Permutation with atleast n unique characters

82 Views Asked by At

I came across this question on Google APAC 2015. I am slightly weak with permutations. The problem goes like this:

There is a password. We know the length of the password and the characters used in the password. The length of the password is n. The number of characters used in the password is m. Each of these characters appears atleast once in the password. How do we find the total number of possible passwords?

I tried to subtract all the permutations of the passwords which had m-1 unique characters, m-2 unique characters and so on from m^n. But how to find the number of passwords of length n with m-1 unique characters in it?

1

There are 1 best solutions below

2
On BEST ANSWER

To solve this sort of problem, use the in-and-out principle aka the inclusion-exclusion principle. The answer to your specific question (the number of surjections from an $n$-element set to an $m$-element set) is $$\binom m0m^n-\binom m1(m-1)^n+\binom m2(m-2)^n-\binom m3(m-3)^n+\cdots.$$

Note that, in order to answer your question, you don't have to determine the number of passwords containing exactly $m-1$ distinct characters. That can also be done with (a more general form of) the in-and-out formula, but it's not needed for your problem.