Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 07 May, 2021
Geek Week

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++




// 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;
}

Java




// 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

Python3




# 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

C#




// 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

PHP




<?php
// PHP program for Arrangement of words
// without changing the relative position
// of vowel and consonants
     
// this function return n!
function factorial($n)
{
    $res = 1;
    for ($i = 1; $i <= $n; $i++)
        $res = $res * $i;
 
    return $res;
}
 
// this will return total
// number of ways
function count1($word)
{
 
    // freq maintains frequency
    // of each character in word
    $freq = array_fill(0, 27, 0);
     
    for($i = 0; $i < 27; $i++)
        $freq[$i] = 0;
 
    $vowel = 0;
    $consonant = 0;
    for ($i = 0; $i < strlen($word); $i++)
    {
        $freq[ord($word[$i]) - 65]++;
 
        // 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
    $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 = 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
    $total = $vowelArrange * $consonantArrange;
    return $total;
}
 
// Driver Code
 
// string contains only
// capital letters
$word = "COMPUTER";
 
// this will contain ans
$ans = count1($word);
echo ($ans);
 
// This code is contributed by mits
?>

Javascript




// Javascript program for Arrangement of words
// without changing the relative position
// of vowel and consonants
     
// this function return n!
function factorial(n)
{
    let res = 1;
    for (let i = 1; i <= n; i++)
        res = res * i;
 
    return res;
}
 
// this will return total
// number of ways
function count1(word)
{
 
    // freq maintains frequency
    // of each character in word
    let freq = new Array(27).fill(0);
     
    for(let i = 0; i < 27; i++)
        freq[i] = 0;
 
    let vowel = 0;
    let consonant = 0;
    for (let i = 0; i < word.length; i++)
    {
        freq[word.charCodeAt(i) - 65]++;
 
        // 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
    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 (let 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
    let total = vowelArrange * consonantArrange;
    return total;
}
 
// Driver Code
 
// string contains only
// capital letters
let word = "COMPUTER";
 
// this will contain ans
let ans = count1(word);
document.write(ans);
 
// This code is contributed by gfgking
Output: 
720

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :