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 at 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 at the remaining positions. We have 7 remaining positions and 5 vowels to fill these 7 places.
Therefore, number of ways to fill vowels = .
Total no. of ways = = 907200
Suppose in a string 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:
- Generate permutations with only adjacent swaps allowed
- First X vowels from a string
- Reverse vowels in a given string
- Modify the string such that it contains all vowels at least once
- Count the pairs of vowels in the given string
- Program to remove vowels from a String
- Remove consecutive vowels from string
- Permutations of a given string using STL
- Replace all consonants with nearest vowels in a string
- Modify string by removing vowels in between two consonants
- Print reverse string after removing vowels
- Check whether the vowels in a string are in alphabetical order or not
- Distinct permutations of the string | Set 2
- All permutations of a string using iteration
- Count the number of vowels occurring in all the substrings of given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.