Given a word containing vowels and consonants. The task is to find that in how many different ways word can be arranged so that the vowels always come together. Given that the length of the word <10
Input: str = "geek" Output: 6 Ways such that both 'e' comes together are 6 i.e. geek, gkee, kgee, eekg, eegk, keeg Input: str = "corporation" Output: 50400
Approach: Since word contains vowels and consonant together. All vowels are needed to remain together then we will take all vowels as a single letter.
As, in the word ‘geeksforgeeks’, we can treat the vowels “eeoee” as one letter.
Thus, we have gksfrgks (eeoee).
This has 9 (8 + 1) letters of which g, k, s each occurs 2 times and the rest are different.
The number of ways arranging these letters = 9!/(2!)x(2!)x(2!) = 45360 ways
Now, 5 vowels in which ‘e’ occurs 4 times and ‘o’ occurs 1 time, can be arranged in 5! /4! = 5 ways.
Required number of ways = (45360 x 5) = 226800
Below is the implementation of the above approach:
Further Optimizations : We can pre-compute required factorial values to avoid re-computations.
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Arrange consonants and vowels nodes in a linked list
- Arrangement of the characters of a word such that all vowels are at odd places
- Ways to arrange Balls such that adjacent balls are of different types
- Count the number of vowels occurring in all the substrings of given string
- Encrypt string with product of number of vowels and consonants in substring of size k
- Count number of ways to divide a number in 4 parts
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Ways to represent a number as a sum of 1's and 2's
- Number of ways to get even sum by choosing three numbers from 1 to N
- Bell Numbers (Number of ways to Partition a Set)
- Total number of ways to place X and Y at n places such that no two X are together
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to cut a stick of length N into K pieces
- Ways to express a number as product of two different factors
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.