I'm working on a Free Code Camp problem - http://www.freecodecamp.com/challenges/bonfire-no-repeats-please
The problem description is as follows -
Return the number of total permutations of the provided string that don't have repeated consecutive letters.
For example, 'aab' should return 2 because it has 6 total permutations, but only 2 of them don't have the same letter (in this case 'a') repeating.
I know I can solve this by writing a program that creates every permutation and then filters out the ones with repeated characters.
But I have this gnawing feeling that I can solve this mathematically.
First question then - Can I?
Second question - If yes, what formula can I use?
To elaborate further -
The example given in the problem is "aab" which the site says has six possible permutations, with only two meeting the non-repeated character criteria:
aab aba baa aab aba baa
The problem sees each character as unique so maybe "aab" could better be described as "a1a2b"
The tests for this problem are as follows (returning the number of permutations that meet the criteria)-
- "aab" should return 2
- "aaa" should return 0
- "abcdefa" should return 3600
- "abfdefa" should return 2640
- "zzzzzzzz" should return 0
I have read through the following posts that have been really helpful in that they have convinced me I should be able to do this mathematically but I'm still in the dark as to how to apply their ideas to my particular problem. - Number of permutations in a word ignoring the consecutive repeated characters and https://answers.yahoo.com/question/index?qid=20110916091008AACH7vf
Hint: Let's assume you have 15 positions and you need to distribute the letter a $5$ times. Then you can achieve this in the following way: Distribute the letter "a" 5 times on 11 positions. Now set an empty space between every pair of consecutive letters.
A specific example: The first distribution might yield
-a-aaa--a--
Adding the spaces [here denoted by an underscore]:
-a_-a_a_a_--a--
In the next step you can distribute the next letter on the remaining 10 positions etc..