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
- Program to remove vowels from a String
- Count the pairs of vowels in the given string
- Remove consecutive vowels from string
- Check whether the vowels in a string are in alphabetical order or not
- Replace all consonants with nearest vowels in a string
- Print reverse string after removing vowels
- Modify string by removing vowels in between two consonants
- Permutations of a given string using STL
- Count the number of vowels occurring in all the substrings of given string
- Program to count vowels in a string (Iterative and Recursive)
- Modify the string by swapping continuous vowels or consonants
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.