Count the number of word pairs with different characters

combinatoricsdiscrete mathematics

Suppose you have an alphabet of fixed size $n$. How many pairs of words of equal length $m$ can you compose such that words in a pair do not have a character in common?

Examples:

Let's call such word pairs unique.

  • $n=2, m=2$: if we only have 2 characters (say A and B) and our words are of length two, then only (AA, BB) and (BB, AA) are unique from 16 possibilities in total.
  • $n=3, m=1$: if the alphabet has 3 characters and the words are singletons, then 6 of them are unique ((A, B), (B, A), etc.).
  • $n=3, m=2$: if the alphabet consists of 3 characters and we are composing words of length 2, there are 18 unique pairs.

One approach would be to count the ways of allocating characters to the left and right words, then counting the number of sequences that can be composed from the allocation. At the moment I cannot wrap my head around taking care of the double counting.

Curious about your suggestions on a better way to look at the problem!

Best Answer

Given your example, both elements of the pair are distinct. I am going by that.

Choose $1$ from $n$ alphabets and make word of $m$ letters. Make other word of the pair from $(n-1)$ remaining letters.

Number of such word pairs $ = \, {n \choose 1} \times 1 \times (n-1)^m$.

Now, choose $2$ from $n$ alphabets and make words of $m$ letters but now we need to be careful as all words just with one letter are already made. So we need to subtract that. We make second word of the pair from remaining (n-2) letters.

Number of such word pairs $ = {n \choose 2} \times (2^m - 2) \times (n-2)^m$.

Similarly when we choose $3$ letters, we would have to exclude all words with just two letters and with $1$ letter. As we try and exclude words with $2$ letters, we would subtract words with $1$ letter more times than we should so we have to keep applying P.I.E.

Now choose $2$ from $n$ alphabets and make words of $m$ letters but now we need to be careful as all words just with one letter are already made. So we need to subtract that.

Number of such word pairs $ = {n \choose 3} \times (3^m - {3 \choose 2} 2^m + {3 \choose 1}) \times (n-3)^m$.

We can convert this into using Stirling Number of the second kind and multiply by number of ways to arrange the letters.

So total number of possible word pairs

$ = \displaystyle \sum \limits_{i = 1}^m {n \choose i} \times (i)! \times S2(m,i) \times (n-i)^m$

Related Question