Permutations of string such that no two vowels are adjacent
Given a string consisting of vowels and consonants. The task is to find the number of ways in which the characters of the string can be arranged such that no two vowels are adjacent to each other.
Note: Given that No. of vowels <= No. of consonants.
Input: str = "permutation" Output : 907200 Input: str = "geeksforgeeks" Output: 3175200
Consider the above example string “permutation”:
- First place all of the consonants in the alternate places like below:
-- p -- r -- m -- t -- t -- n --
- Number of ways to place consonants = 6! / 2!. as appears twice and should be considered once.
- Then place the vowels in the remaining positions. We have 7 remaining positions and 5 vowels to fill these 7 places.
Therefore, the number of ways to fill vowels = .
Total no. of ways = = 907200
Suppose, in a string, the number of vowels is vowelCount and the number of consonants is consonantCount.
Total ways = (consonantCount! / duplicateConsonant!) * C(consonantCount+1 , vowelCount) * (vowelCount! / duplicateVowel!)
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.