Arrangement of words without changing the relative position of vowel and consonants

Given a word of length less than 10, the task is to find a number of ways in which it can be arranged without changing the relative position of vowel and consonants.

Examples:

Input: "GEEKS"
Output: 6

Input: "COMPUTER"
Output: 720


Approach:

  1. Count the vowels and consonants in the word
  2. Now find total number of ways to arrange vowel only
  3. Then find ways to arrange consonant only.
  4. Multiply both answer to get the Total ways = (no of ways to arrange vowel only)*(no of ways to arrange consonant only)

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for Arrangement of words
// without changing the relative position of 
// vowel and consonants 
#include <bits/stdc++.h>
using namespace std;
  
#define ll long int
  
// this function return n!
ll factorial(ll n)
{
    ll res = 1;
    for (int i = 1; i <= n; i++)
        res = res * i;
  
    return res;
}
  
// this will return total number of ways
ll count(string word)
{
  
    // freq maintains frequency
    // of each character in word
    ll freq[27] = { 0 };
  
    ll vowel = 0, consonant = 0;
    for (int i = 0; i < word.length(); i++) {
        freq[word[i] - 'A']++;
  
        // check character is vowel or not
        if (word[i] == 'A' || word[i] == 'E'
            || word[i] == 'I'
            || word[i] == 'O' || word[i] == 'U') {
            vowel++;
        }
  
        // the characters that are not vowel
        // must be consonant
        else
            consonant++;
    }
  
    // number of ways to arrange vowel
    ll vowelArrange;
    vowelArrange = factorial(vowel);
    vowelArrange /= factorial(freq[0]);
    vowelArrange /= factorial(freq[4]);
    vowelArrange /= factorial(freq[8]);
    vowelArrange /= factorial(freq[14]);
    vowelArrange /= factorial(freq[20]);
  
    ll consonantArrange;
    consonantArrange = factorial(consonant);
    for (int i = 0; i < 26; i++) {
        if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20)
            consonantArrange /= factorial(freq[i]);
    }
  
    // multiply both as these are independent
    ll total = vowelArrange * consonantArrange;
    return total;
}
  
// Driver function
int main()
{
    // string contains only
    // capital letters
    string word = "COMPUTER";
  
    // this will contain ans
    ll ans = count(word);
    cout << ans << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for Arrangement of words
// without changing the relative position of 
// vowel and consonants 
  
class GFG
{
      
    // this function return n!
    static long factorial(long n)
    {
        long res = 1;
        for (int i = 1; i <= n; i++)
            res = res * i;
      
        return res;
    }
      
    // this will return total number of ways
    static long count(String word)
    {
      
        // freq maintains frequency
        // of each character in word
        int freq[] =new int[27];
          
        for(int i=0;i<27;i++)
            freq[i]=0;
      
        long vowel = 0, consonant = 0;
        for (int i = 0; i < word.length(); i++) {
            freq[word.charAt(i) - 'A']++;
      
            // check character is vowel or not
            if (word.charAt(i) == 'A' || word.charAt(i) == 'E'
                || word.charAt(i) == 'I'
                || word.charAt(i) == 'O' || word.charAt(i) == 'U') {
                vowel++;
            }
      
            // the characters that are not vowel
            // must be consonant
            else
                consonant++;
        }
      
        // number of ways to arrange vowel
        long vowelArrange;
        vowelArrange = factorial(vowel);
        vowelArrange /= factorial(freq[0]);
        vowelArrange /= factorial(freq[4]);
        vowelArrange /= factorial(freq[8]);
        vowelArrange /= factorial(freq[14]);
        vowelArrange /= factorial(freq[20]);
      
        long consonantArrange;
        consonantArrange = factorial(consonant);
        for (int i = 0; i < 26; i++) {
            if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20)
                consonantArrange /= factorial(freq[i]);
        }
      
        // multiply both as these are independent
        long total = vowelArrange * consonantArrange;
        return total;
    }
      
    // Driver function
    public static void main(String []args)
    {
        // string contains only
        // capital letters
        String word = "COMPUTER";
      
        // this will contain ans
        long ans = count(word);
        System.out.println(ans);
  
    }
  
}
  
// This code is contributed by ihritik

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for Arrangement of words
# without changing the relative position of 
# vowel and consonants 
  
# this function return n!
def factorial(n):
    res = 1
    for i in range(1, n + 1):
        res = res * i
    return res
  
# this will return total number of ways 
def count(word):
      
    # freq maintains frequency
    # of each character in word 
    freq = [0 for i in range(30)]
    vowel = 0
    consonant = 0
    for i in range(len(word)):
        freq[ord(word[i]) -65 ] += 1
          
        # check character is vowel or not 
        if(word[i] == 'A'or word[i] == 'E' or 
           word[i] == 'I' or word[i] == 'O'or 
           word[i] == 'U'):
            vowel += 1
  
        # the characters that are not 
        # vowel must be consonant
        else:
            consonant += 1
  
    # number of ways to arrange vowel
    vowelArrange = factorial(vowel)
    vowelArrange //= factorial(freq[0])
    vowelArrange //= factorial(freq[4])
    vowelArrange //= factorial(freq[8])
    vowelArrange //= factorial(freq[14])
    vowelArrange //= factorial(freq[20])
  
    consonantArrange = factorial(consonant)
    for i in range(26):
        if(i != 0 and i != 4 and i != 8 and 
           i != 14 and i != 20):
            consonantArrange//= factorial(freq[i])
  
    # multiply both as these are independent
    total = vowelArrange * consonantArrange
    return total
  
# Driver code
  
# string contains only
# capital letters
word = "COMPUTER"
  
# this will contain ans
ans = count(word)
print(ans)
  
# This code is contributed 
# by sahilshelangia

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for Arrangement of words
// without changing the relative position of 
// vowel and consonants 
using System;
class GFG
{
      
    // this function return n!
    static long factorial(long n)
    {
        long res = 1;
        for (int i = 1; i <= n; i++)
            res = res * i;
      
        return res;
    }
      
    // this will return total number of ways
    static long count(string word)
    {
      
        // freq maintains frequency
        // of each character in word
        int []freq =new int[27];
          
        for(int i=0;i<27;i++)
            freq[i]=0;
      
        long vowel = 0, consonant = 0;
        for (int i = 0; i < word.Length; i++) {
            freq[word[i] - 'A']++;
      
            // check character is vowel or not
            if (word[i] == 'A' || word[i] == 'E'
                || word[i] == 'I'
                || word[i] == 'O' || word[i] == 'U') {
                vowel++;
            }
      
            // the characters that are not vowel
            // must be consonant
            else
                consonant++;
        }
      
        // number of ways to arrange vowel
        long vowelArrange;
        vowelArrange = factorial(vowel);
        vowelArrange /= factorial(freq[0]);
        vowelArrange /= factorial(freq[4]);
        vowelArrange /= factorial(freq[8]);
        vowelArrange /= factorial(freq[14]);
        vowelArrange /= factorial(freq[20]);
      
        long consonantArrange;
        consonantArrange = factorial(consonant);
        for (int i = 0; i < 26; i++) {
            if (i != 0 && i != 4 && i != 8 && i != 14 && i != 20)
                consonantArrange /= factorial(freq[i]);
        }
      
        // multiply both as these are independent
        long total = vowelArrange * consonantArrange;
        return total;
    }
      
    // Driver function
    public static void Main()
    {
        // string contains only
        // capital letters
        string word = "COMPUTER";
      
        // this will contain ans
        long ans = count(word);
        Console.WriteLine(ans);
  
    }
  
}
// This code is contributed by ihritik

chevron_right


PHP

Output:

720


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.