Number of ways to arrange a word such that all vowels occur together - GeeksforGeeks

06 May, 2021
Given a word containing vowels and consonants. The task is to find that in how many ways to 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 consonants 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 waysNow, 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: 
C++
Java
Python3
C#
Javascript
// C++ program to calculate the no. of ways// to arrange the word having vowels together#include <bits/stdc++.h>#define ll long long intusing namespace std; // Factorial of a numberll fact(int n){    ll f = 1;    for (int i = 2; i <= n; i++)        f = f * i;    return f;} // calculating ways for arranging consonantsll 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 vowelsll 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 waysll 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 codeint main(){    string str = "geeksforgeeks";     cout << countWays(str) << endl;     return 0;}
// Java program to calculate the no. of// ways to arrange the word having// vowels togetherimport java.util.*;  class GFG{  // Factorial of a numberstatic int fact(int n){    int f = 1;    for(int i = 2; i <= n; i++)        f = f * i;              return f;}  // Calculating ways for arranging consonantsstatic int waysOfConsonants(int size1,                            int []freq){    int 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 vowelsstatic int 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 waysstatic int countWays(String str){    int []freq = new int [200];    for(int i = 0; i < 200; i++)        freq[i] = 0;              for(int i = 0; i < str.length(); i++)        freq[str.charAt(i) - 'a']++;              // Count vowels and consonant    int vowel = 0, consonant = 0;    for(int i = 0; i < str.length(); i++)    {        if (str.charAt(i) != 'a' && str.charAt(i) != 'e' &&            str.charAt(i) != 'i' && str.charAt(i) != 'o' &&            str.charAt(i) != 'u')            consonant++;        else            vowel++;    }      // Total no. of ways    return waysOfConsonants(consonant + 1, freq) *           waysOfVowels(vowel, freq);}  // Driver codepublic static void main(String []args){    String str = "geeksforgeeks";      System.out.println(countWays(str));}} // This code is contributed by rutvik_56
# Python3 program to calculate# the no. of ways to arrange# the word having vowels together # Factorial of a numberdef fact(n):     f = 1    for i in range(2, n + 1):        f = f * i    return f # calculating ways for# arranging consonantsdef waysOfConsonants(size1, freq):     ans = fact(size1)    for i in range(26):         # Ignore vowels        if (i == 0 or i == 4 or            i == 8 or i == 14 or            i == 20):            continue        else:            ans = ans // fact(freq[i])     return ans # calculating ways for# arranging vowelsdef waysOfVowels(size2, 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 waysdef countWays(str1):     freq = [0] * 26    for i in range(len(str1)):        freq[ord(str1[i]) -             ord('a')] += 1     # Count vowels and consonant    vowel = 0    consonant = 0    for i in range(len(str1)):         if (str1[i] != 'a' and str1[i] != 'e' and            str1[i] != 'i' and str1[i] != 'o' and            str1[i] != 'u'):            consonant += 1        else:            vowel += 1     # total no. of ways    return (waysOfConsonants(consonant + 1, freq) *            waysOfVowels(vowel, freq)) # Driver codeif __name__ == "__main__":     str1 = "geeksforgeeks"    print(countWays(str1)) # This code is contributed by Chitranayal
// C# program to calculate the no. of// ways to arrange the word having// vowels togetherusing System.Collections.Generic;using System; class GFG{ // Factorial of a numberstatic int fact(int n){    int f = 1;    for(int i = 2; i <= n; i++)        f = f * i;             return f;} // Calculating ways for arranging consonantsstatic int waysOfConsonants(int size1,                            int []freq){    int 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 vowelsstatic int 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 waysstatic int countWays(string str){    int []freq = new int [200];    for(int i = 0; i < 200; i++)        freq[i] = 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 codepublic static void Main(){    string str = "geeksforgeeks";     Console.WriteLine(countWays(str));}} // This code is contributed by Stream_Cipher
<script>// Javascript program to calculate the no. of// ways to arrange the word having// vowels together     // Factorial of a numberfunction fact(n){       let f = 1;    for(let i = 2; i <= n; i++)        f = f * i;               return f;} // Calculating ways for arranging consonantsfunction waysOfConsonants(size1,freq){    let ans = fact(size1);    for(let i = 0; i < 26; i++)    {                   // Ignore vowels        if (i == 0 || i == 4 || i == 8 ||            i == 14 || i == 20)            continue;        else            ans = Math.floor(ans / fact(freq[i]));    }    return ans;} // Calculating ways for arranging vowelsfunction waysOfVowels(size2,freq){    return Math.floor(fact(size2) / (fact(freq[0]) *          fact(freq[4]) * fact(freq[8]) *         fact(freq[14]) * fact(freq[20])));} // Function to count total no. of waysfunction countWays(str){    let freq = new Array(200);    for(let i = 0; i < 200; i++)        freq[i] = 0;               for(let i = 0; i < str.length; i++)        freq[str[i].charCodeAt(0) - 'a'.charCodeAt(0)]++;               // Count vowels and consonant    let vowel = 0, consonant = 0;    for(let 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 codelet str = "geeksforgeeks";document.write(countWays(str));              // This code is contributed by avanitrachhadiya2155</script>
226800
 
Further Optimizations: We can pre-compute required factorial values to avoid re-computations.
ukasp
Stream_Cipher
rutvik_56
avanitrachhadiya2155
factorial
vowel-consonant
Combinatorial
Mathematical
Mathematical
Combinatorial
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Combinational Sum
Count ways to reach the nth stair using step 1, 2 or 3
Print all possible strings of length k that can be formed from a set of n characters
Count of subsets with sum equal to X
Python program to get all subsets of given size of a set
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
C++ Data Types
Coin Change | DP-7
Merge two sorted arrays