This question comes from Stack Overflow. I feel that we need more of a mathematical breakthrough, so I forward the question here.
I also found a similar problem that seems to be a special case of this problem.
In short, I want to shuffle a known string. This string will have any number of repeated letters. However, I must not allow the same character to appear n consecutive times in the shuffled string. For example, for the string "aaaabbbcc" and n=2, "aabaabbcc" is a correct permutation, but "aaabbbcca" is not because a letter appears more than twice in a row.
The question is: How to design this shuffling algorithm to ensure that each valid string has the same probability of occurrence?
A naive idea is to review the shuffled results and reshuffle them if they don't meet the requirements. However, for some extreme cases, performance may be extremely low. For example, "aaaabbbbbbbbbb" and n=2. In this case, only "bbabbabbabbabbabb" is the correct output, and all other randomly generated sequences are discarded. In this way, the function will continue to loop until the unique correct string is generated with a very small probability.
Another idea is to generate all possible permutations, then eliminate those that are wrong and draw randomly from the remaining correct permutations. But the complexity of this algorithm will be factorial, and the number of possible permutations will soon exceed the upper limit of 64-bit unsigned integer.
The final idea is to make a mathematical modification of Fisher-Yates: start with a random character, and then multiply the probability of the previous character being drawn by an attenuation coefficient each time the next character is drawn. For example, if the previous character has been consecutively drawn for n times, the attenuation coefficient should become 0 to ensure that the next character cannot be the same as the previous one. However, I do not know how to calculate this attenuation coefficient exactly to ensure that all the correct permutations are generated with equal probability.