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:
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.
- Longest substring of vowels with no two adjacent alphabets same
- 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
- Permutations of a given string using STL
- Remove consecutive vowels from string
- Program to duplicate Vowels in String
- Program to remove vowels from a String
- Count the pairs of vowels in the given string
- Longest Subsequence of a String containing only vowels
- All permutations of a string using iteration
- Distinct permutations of the string | Set 2
- Check whether the vowels in a string are in alphabetical order or not
- Replace all consonants with nearest vowels in a string
- Replace every vowels with lexicographically next vowel in a String
- Print reverse string after removing vowels
- Modify string by removing vowels in between two consonants
- Count the number of vowels occurring in all the substrings of given string
- Minimum cost to convert given string to consist of only vowels
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.