2

Say we have a string with repeated letters, 'abcaacb', and we want to determine it's rank alphabetically among other strings that contain the same exact letters. So rank 1 would be 'aaabbcc', and the highest rank given would be 'ccbbaaa' (a number that could be generated using the multinomial theorem). I've found that it's pretty straight-forward to find the rank of a non-repeating character string like 'adcb' by first sorting the string to 'abcd', then comparing letter by letter and summing factorials (as the general idea). This is a question on Stack Overflow, and you can see it here.

I've tried doing something similar to this, by using the multinomial theorem instead, to avoid the repeated strings, but the result is incorrect for repeated strings, but still correct for non-repeating strings.

I figured there must be a math formula out there to do this, but I've yet to come across it. I would assume it's some kind of summation, and uses the multinomial theorem somehow, but again, I've had no luck finding it, or figuring it out on my own.

Also, as a note, it would be helpful if this could be done mathematically (that's why it's posted here). The simple solution would be to generate all permutations and then search them for the index of the one we're looking for. This would be incredibly inefficient of course.

Any help or suggestions is greatly appreciated.

Matt
  • 231

0 Answers0