Disclaimer: I haven't done math in a decade. Sorry if this is not very scientific
Hello,
I am writing a computer program, that is supposed to return the number of all permutations of an input string, excluding the permutations where a letter is repeated consecutively. Ex. : Input is "aabc", valid permutations are "abac", "abca" invalid "baac", "bcaa".
My calculation looks like this:
- Take all permutations: 4!
- Take all permutations where "a" is repeated, treating the repeated character as one group. ex. [aa]bc, b[aa]c. In this case it's 3! - (length of string - group length + 1)
- Take all permutations of the letters within the group. ex. [a1a2]bc, [a2a1]bc. In this case 2! (group length)!
So the formula is 4! - (3! * 2!).
This seems to work as long as there is only one repeated character. However as soon as there is more than one, if I repeat the algorithm for all characters, I will discount some permutations twice (the ones that have both character repeated). Ex. input is: "aabbcd" "abbacd" is discounted once. "cdaabb" is discounted twice.
So I need an additional correction where I take the union of the results of the two algorithms and add it back.
Essentially: Out of all permutations of the string "aabbcd", what is the number of strings that have both "aa" and "bb" in them?