Number of ways to arrange a word such that no vowels occur together
Given an English word of length at most 20 characters. Calculate the number of ways to arrange the word such that no vowels occur together.
Note: If the total number of vowels in the given word is one then the result should be 0.
Input : allahabad Output : 7200 Input : geeksforgeeks Output : 32205600 Input : abcd Output : 0
Since the word contains vowels and consonants. Calculate the total number of ways to arrange the given word and subtract the number of ways having all vowels together. To calculate the total number of ways we’ll use the following formula-
No of ways = (n!) / (r1! * r2! * ... * rk!)
Where n is the number of different characters in the word and r1, r2 … rk, are the frequency of same type character.
In order to calculate the number of ways such that all vowels occur together, we consider the group of all vowels as a single character and using the above formula we’ll calculate the total number of ways having all vowel together. Now subtract it from the total number of ways to get the result.
Below is the implementation of the above approach:
7200 32205600 0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.