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

**Examples:**

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 havegksfrgks (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:

`// CPP program to calculate the no. of ways ` `// to arrange the word having vowels together ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Factorial of a number ` `ll fact(` `int` `n) ` `{ ` ` ` `ll f = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `f = f * i; ` ` ` `return` `f; ` `} ` ` ` `// calculating ways for arranging consonants ` `ll waysOfConsonants(` `int` `size1, ` `int` `freq[]) ` `{ ` ` ` `ll ans = fact(size1); ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { ` ` ` ` ` `// Ignore vowels ` ` ` `if` `(i == 0 || i == 4 || i == 8 || i == 14 || i == 20) ` ` ` `continue` `; ` ` ` `else` ` ` `ans = ans / fact(freq[i]); ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// calculating ways for arranging vowels ` `ll waysOfVowels(` `int` `size2, ` `int` `freq[]) ` `{ ` ` ` `return` `fact(size2) / (fact(freq[0]) * fact(freq[4]) * fact(freq[8]) ` ` ` `* fact(freq[14]) * fact(freq[20])); ` `} ` ` ` `// Function to count total no. of ways ` `ll countWays(string str) ` `{ ` ` ` ` ` `int` `freq[26] = { 0 }; ` ` ` `for` `(` `int` `i = 0; i < str.length(); i++) ` ` ` `freq[str[i] - ` `'a'` `]++; ` ` ` ` ` `// Count vowels and consonant ` ` ` `int` `vowel = 0, consonant = 0; ` ` ` `for` `(` `int` `i = 0; i < str.length(); i++) { ` ` ` ` ` `if` `(str[i] != ` `'a'` `&& str[i] != ` `'e'` `&& str[i] != ` `'i'` ` ` `&& str[i] != ` `'o'` `&& str[i] != ` `'u'` `) ` ` ` `consonant++; ` ` ` `else` ` ` `vowel++; ` ` ` `} ` ` ` ` ` `// total no. of ways ` ` ` `return` `waysOfConsonants(consonant+1, freq) * ` ` ` `waysOfVowels(vowel, freq); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"geeksforgeeks"` `; ` ` ` ` ` `cout << countWays(str) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

226800

**Further Optimizations : ** We can pre-compute required factorial values to avoid re-computations.

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.

## Recommended Posts:

- Number of ways to arrange a word such that no vowels occur together
- Count number of ways to arrange first N numbers
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- 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
- Number of ways to arrange K different objects taking N objects at a time
- Arrangement of the characters of a word such that all vowels are at odd places
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Ways to arrange Balls such that adjacent balls are of different types
- Arrange given numbers to form the smallest number
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Count of substrings consisting of even number of vowels
- Sort an Array of Strings according to the number of Vowels in them
- Check whether all the substrings have number of vowels atleast as that of consonants
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Count the number of vowels occurring in all the substrings of given string
- Lexicographically smallest K-length substring containing maximum number of vowels
- Encrypt string with product of number of vowels and consonants in substring of size k
- Count number of rotated strings which have more number of vowels in the first half than second half

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.