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 C++ implementation of the above approach:

filter_none

editclose

play_arrow

linkbrightness_4code

#include <bits/stdc++.h>

#define ll long long int

usingnamespacestd;

// Function to check if a character is vowel or consonent

boolisVowel(charch)

{

if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u')

returntrue;

else

returnfalse;

}

// Function to calculate factorial of a number

ll fact(ll n)

{

if(n < 2)

return1;

returnn * 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(autoitr = freq.begin(); itr != freq.end(); itr++) {

if(isVowel(itr->first)) {

denom *= fact(itr->second);

cnt_vwl += itr->second;

}

}

returnfact(cnt_vwl) / denom;

}

// calculating no of ways to arrange the given word such that all 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 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.