Pantalaimon
Pantalaimon

Reputation: 596

Algorithm to find number of possible string variations

For a password related project, I'm looking for an algorithm that calculates the number of possible variations a certain string can have based on a few options. For now the string variation options are upper/lowercase and character to number replacements (like E=3)

For example, lets take the string 'abc@def'

With just upper/lower variations, there are 6 characters that can vary, and the total number of possible variations is 2^6 = 64.

With just character to number replacements, there are 2 characters that qualify (A=4,E=3). That makes the number of variations 2^2 = 4.

I'm struggeling with calculating the number of variations when both methods are enabled. I've tried (2^6 * 2^4), but obviously this doesn't consider the overlap that occurs when applying both.

For example, the variations 'abc@def' and 'abc@dEf' both result in 'abc@d3f' with number substitution on de character E and should be counted as one.

Somehow I can't figure this out :)

Upvotes: 2

Views: 420

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186718

Just count all possibilities for each letter within the password and multiply them together:

  letter options count
     a    a A 4      3
     b      b B      2 
     c      c C      2
     @        @      1 
     d      d D      2
     e    e E 3      3
     f      f F      2

Finally we have 3 * 2 * 2 * 1 * 2 * 3 * 2 == 144 variants

Upvotes: 2

Related Questions