Given an English word of length at most 20 characters. Calculate the number of ways to arrange the word such that no vowels occur together.

**Note **: If the total number of vowels in the given word is one then the result should be 0.

**Examples:**

Input :allahabadOutput :7200Input :geeksforgeeksOutput :32205600Input :abcdOutput :0

Since the word contains vowels and consonants. Calculate the total number of ways to arrange the given word and subtract the number of ways having all vowels together. To calculate the total number of ways we’ll use the following formula-

No of ways = (n!) / (r1! * r2! * ... * rk!)

Where n is the number of different characters in the word and r1, r2 … rk, are the frequency of same type character.

In order to calculate the number of ways such that all vowels occur together, we consider the group of all vowels as a single character and using the above formula we’ll calculate the total number of ways having all vowel together. Now subtract it from the total number of ways to get the result.

Below is the implementation of the above approach:

## C++

`#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to check if a character is vowel or consonent ` `bool` `isVowel(` `char` `ch) ` `{ ` ` ` `if` `(ch == ` `'a'` `|| ch == ` `'e'` `|| ch == ` `'i'` `|| ch == ` `'o'` `|| ch == ` `'u'` `) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` `} ` ` ` `// Function to calculate factorial of a number ` `ll fact(ll n) ` `{ ` ` ` `if` `(n < 2) ` ` ` `return` `1; ` ` ` `return` `n * fact(n - 1); ` `} ` ` ` `// Calculating no of ways for arranging vowels ` `ll only_vowels(map<` `char` `, ` `int` `>& freq) ` `{ ` ` ` `ll denom = 1; ` ` ` `ll cnt_vwl = 0; ` ` ` ` ` `// Iterate the map and count the number of vowels and calculate ` ` ` `// no of ways to arrange vowels ` ` ` `for` `(` `auto` `itr = freq.begin(); itr != freq.end(); itr++) { ` ` ` `if` `(isVowel(itr->first)) { ` ` ` `denom *= fact(itr->second); ` ` ` `cnt_vwl += itr->second; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `fact(cnt_vwl) / denom; ` `} ` ` ` `// calculating no of ways to arrange the given word such that all vowels ` `// come together ` `ll all_vowels_together(map<` `char` `, ` `int` `>& freq) ` `{ ` ` ` `// calculate no of ways to arrange vowels ` ` ` `ll vow = only_vowels(freq); ` ` ` ` ` `// to store denominator of fraction ` ` ` `ll denom = 1; ` ` ` ` ` `// count of consonents ` ` ` `ll cnt_cnst = 0; ` ` ` ` ` `for` `(` `auto` `itr = freq.begin(); itr != freq.end(); itr++) { ` ` ` `if` `(!isVowel(itr->first)) { ` ` ` `denom *= fact(itr->second); ` ` ` `cnt_cnst += itr->second; ` ` ` `} ` ` ` `} ` ` ` ` ` `// calculate the number of ways to arrange the word such that ` ` ` `// all vowels come together ` ` ` `ll ans = fact(cnt_cnst + 1) / denom; ` ` ` ` ` `return` `(ans * vow); ` `} ` ` ` `// To calculate total number of permutations ` `ll total_permutations(map<` `char` `, ` `int` `>& freq) ` `{ ` ` ` `// To store length of the given word ` ` ` `ll cnt = 0; ` ` ` ` ` `// denominator of fraction ` ` ` `ll denom = 1; ` ` ` ` ` `for` `(` `auto` `itr = freq.begin(); itr != freq.end(); itr++) { ` ` ` `denom *= fact(itr->second); ` ` ` `cnt += itr->second; ` ` ` `} ` ` ` ` ` `// retur total number of permutations of the given word ` ` ` `return` `fact(cnt) / denom; ` `} ` ` ` `// Function to calculate number of permutations such that ` `// no vowels come together ` `ll no_vowels_together(string& word) ` `{ ` ` ` `// to store frequency of character ` ` ` `map<` `char` `, ` `int` `> freq; ` ` ` ` ` `// count frequency of all characters ` ` ` `for` `(` `int` `i = 0; i < word.size(); i++) { ` ` ` `char` `ch = ` `tolower` `(word[i]); ` ` ` `freq[ch]++; ` ` ` `} ` ` ` ` ` `// calculate total number of permutations ` ` ` `ll total = total_permutations(freq); ` ` ` ` ` `// calculate total number of permutations such that ` ` ` `// all vowels come together ` ` ` `ll vwl_tgthr = all_vowels_together(freq); ` ` ` ` ` `// substrat vwl_tgthr from total to get the result ` ` ` `ll res = total - vwl_tgthr; ` ` ` ` ` `// return the result ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `string word = ` `"allahabad"` `; ` ` ` `ll ans = no_vowels_together(word); ` ` ` `cout << ans << endl; ` ` ` ` ` `word = ` `"geeksforgeeks"` `; ` ` ` `ans = no_vowels_together(word); ` ` ` `cout << ans << endl; ` ` ` ` ` `word = ` `"abcd"` `; ` ` ` `ans = no_vowels_together(word); ` ` ` `cout << ans << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code for above approach ` ` ` `# Function to check if a character is ` `# vowel or consonent ` `def` `isVowel(ch): ` ` ` `if` `(ch ` `=` `=` `'a'` `or` `ch ` `=` `=` `'e'` `or` ` ` `ch ` `=` `=` `'i'` `or` `ch ` `=` `=` `'o'` `or` `ch ` `=` `=` `'u'` `) : ` ` ` `return` `True` ` ` `else` `: ` ` ` `return` `False` ` ` `# Function to calculate factorial of a number ` `def` `fact(n): ` ` ` `if` `(n < ` `2` `): ` ` ` `return` `1` ` ` `return` `n ` `*` `fact(n ` `-` `1` `) ` ` ` `# Calculating no of ways for arranging vowels ` `def` `only_vowels(freq): ` ` ` ` ` `denom ` `=` `1` ` ` `cnt_vwl ` `=` `0` ` ` ` ` `# Iterate the map and count the number of ` ` ` `# vowels and calculate no of ways to arrange vowels ` ` ` `for` `itr ` `in` `freq: ` ` ` `if` `(isVowel(itr)): ` ` ` `denom ` `*` `=` `fact(freq[itr]) ` ` ` `cnt_vwl ` `+` `=` `freq[itr] ` ` ` ` ` `return` `fact(cnt_vwl) ` `/` `/` `denom ` ` ` `# calculating no of ways to arrange the given word ` `# such that vowels come together ` `def` `all_vowels_together(freq): ` ` ` ` ` `# calculate no of ways to arrange vowels ` ` ` `vow ` `=` `only_vowels(freq) ` ` ` ` ` `# to store denominator of fraction ` ` ` `denom ` `=` `1` ` ` ` ` `# count of consonents ` ` ` `cnt_cnst ` `=` `0` ` ` ` ` `for` `itr ` `in` `freq: ` ` ` `if` `(isVowel(itr) ` `=` `=` `False` `): ` ` ` `denom ` `*` `=` `fact(freq[itr]) ` ` ` `cnt_cnst ` `+` `=` `freq[itr] ` ` ` ` ` `# calculate the number of ways to arrange ` ` ` `# the word such that vowels come together ` ` ` `ans ` `=` `fact(cnt_cnst ` `+` `1` `) ` `/` `/` `denom ` ` ` ` ` `return` `(ans ` `*` `vow) ` ` ` `# To calculate total number of permutations ` `def` `total_permutations(freq): ` ` ` ` ` `# To store length of the given word ` ` ` `cnt ` `=` `0` ` ` ` ` `# denominator of fraction ` ` ` `denom ` `=` `1` ` ` ` ` `for` `itr ` `in` `freq: ` ` ` `denom ` `*` `=` `fact(freq[itr]) ` ` ` `cnt ` `+` `=` `freq[itr] ` ` ` ` ` `# retur total number of permutations ` ` ` `# of the given word ` ` ` `return` `fact(cnt) ` `/` `/` `denom ` ` ` `# Function to calculate number of permutations ` `# such that no vowels come together ` `def` `no_vowels_together(word): ` ` ` ` ` `# to store frequency of character ` ` ` `freq ` `=` `dict` `() ` ` ` ` ` `# count frequency of acharacters ` ` ` `for` `i ` `in` `word: ` ` ` `ch ` `=` `i.lower() ` ` ` `freq[ch] ` `=` `freq.get(ch, ` `0` `) ` `+` `1` ` ` ` ` `# calculate total number of permutations ` ` ` `total ` `=` `total_permutations(freq) ` ` ` ` ` `# calculate total number of permutations ` ` ` `# such that vowels come together ` ` ` `vwl_tgthr ` `=` `all_vowels_together(freq) ` ` ` ` ` `# substrat vwl_tgthr from total ` ` ` `# to get the result ` ` ` `res ` `=` `total ` `-` `vwl_tgthr ` ` ` ` ` `# return the result ` ` ` `return` `res ` ` ` `# Driver code ` `word ` `=` `"allahabad"` `ans ` `=` `no_vowels_together(word) ` `print` `(ans) ` ` ` `word ` `=` `"geeksforgeeks"` `ans ` `=` `no_vowels_together(word) ` `print` `(ans) ` ` ` `word ` `=` `"abcd"` `ans ` `=` `no_vowels_together(word) ` `print` `(ans) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

7200 32205600 0

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 all 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
- Count the number of vowels occurring in all the substrings of given string
- Number of words that can be made using exactly P consonants and Q vowels from the 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.