I'm looking for the equation to determine the index of a permutation with repetition with known parameters.
For example: A total of $9$ values, $4$ A's and $5$ B's Gives a total of $126$ permutations with repetition. $$\frac{9!}{4! \cdot 5!} = 126$$
The Zero-based lexicographical order goes from 0 = AAAABBBBB to 125 = BBBBBAAAA This data set is trivial enough that I just generated all the values with code, but large data sets are impractical. I know that index 76 = BABABABAB since I have a list of answers, but I don't want to generate a partial or a full list.
How do I direclty convert any sequence such as BABABABAB to the permutation with repetition index? How do I direclty do the reverse and convert the permutation with repetition index back to the sequence?
I'm looking for the equations / methods to use in a non-trivial example.
Lexicographical order is prefered, but not required as long as the method can convert in both directions (Sequence => Index and Index => Sequence).
Forward conversion was explained in "Lexicographical rank of a string with duplicate characters". In short, I'm referencing the other answer from that question:
I've implemented this in python and tested it on your example: (proof of concept)
which returns the expected result:
and should run in $O(m\cdot n)$ where $m$ is the number of unique chars among the $n$ chars.
The backward conversion uses the same idea. This time around, we are fixing characters from smallest to largest and counting the possible permutations until the count exceeds our index, until we fix (find) every character.
This was additionally explained and implemented in:
"Find n-th lexicographically permutation of a string | Set 2" from geeksforgeeks.org.
Algorithm for finding multiset permutation given lexicographic index on StackOverflow.