[Math] How many strings of five ASCII characters contain the character @ (“at” sign) at least once

combinatoricsdiscrete mathematics

I'm given the question: "How many strings of five ASCII characters contain the
character @ (“at” sign) at least once?"

Note: There are 128 different ASCII characters.

I realized I'd have to use rule of product and sum on this one right away. I approached it by figuring out 5 cases and summing them to get the answer.

Case I:

@ is contained once in the string

I have to pick the position of @ which can be done in 5 ways. Then I have to pick the remaining 4 characters which can be done in $127^4$ ways.

$5 * 127^4$ ways to do this case

Case II:

I have to pick the position of the first @ (5 ways to do that) then the position of the second @ (4 ways to do that) then $127^3$ ways to pick the remaining characters from the string. $5*4*127^3$ ways to do this step

Case III:

I have to pick the position of the first second and third @. Then I have to pick the remaining $2$ characters. $5*4*3*127^2$ ways to do this step.

Case IV:

I have to pick the position of first,second,third, and fourth @. Then I have to pick the last character. $5*4*3*2*127$ ways to complete this step.

Case V:

The whole string is @. Only one way to do this step.

I summed all my cases and the result was $1,342,673,846$ the back of the book gave the answer $1,321,368,961$. Where did I go wrong?

Best Answer

In your cases $II, III, IV$ you have overcounted because swapping the $@$ signs results in the same string. So in case $II$, instead of $5 \cdot 4$ ways to choose where the $@$ signs are, there are $5 \choose 2$ The fact that you are close supports that there are $128$ total ASCII characters

Less work is to compute how many total strings there are and subtract the ones with no $@$. $128^5-127^5=1,321,368,961$