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
- Modify the string such that it contains all vowels at least once
- Reverse vowels in a given string
- Count the pairs of vowels in the given string
- Remove consecutive vowels from string
- Program to duplicate Vowels in String
- Program to remove vowels from a String
- Permutations of a given string using STL
- Check whether the vowels in a string are in alphabetical order or not
- Modify string by removing vowels in between two consonants
- Print reverse string after removing vowels
- Replace all consonants with nearest vowels in a string
- All permutations of a string using iteration
- Distinct permutations of the string | Set 2
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.