Number of ways to arrange a word such that all vowels occur together
Given a word containing vowels and consonants. The task is to find that in how many ways the 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 ways
Now, 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
Algorithm:
Step 1: Start
Step 2: Create a static function named “fact” of int return type and take the input value of int type as input which returns the factorial of a number.
Step 3: Now, create another static function of int return type named “waysOfConsonanats taking two parameters as input integer say “size1” and an integer array say “freq”.
a. inside it let’s calculate the factorial of a given value that is sez1.
b. Start a loop and iterate it 26 times and if the current value is a vowel, then continue the iteration else divide its c calculated factorial by the given frequency in the freq array
c. return the final value
Step 4: Now, create another static function of int return type named “waysOfVowels taking two parameters as input integer say size2” and an integer array say “freq”.
a. Calculate the factorial of “size2” inside of “waysOfVowels” and divide it by the sum of the factorials for each vowel’s frequency.
Step 5: Create a static method called “countWays” that returns the overall number of possible word arrangements and accepts the string parameter “str.”
a. Construct the 26-element “freq” integer array with all of its elements set to 0.
b. Based on the ASCII value of the characterless “a,” loop over the characters in the string and increase the value of the appropriate “freq” element.
c. The string’s vowels and consonants should be counted.
d. By calling “waysOfConsonants” and “waysOfVowels” with the right parameters and multiplying the results, you can get the overall number of possible ways.
Step 6: Return the final value
Step 7: End
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll fact( int n)
{
ll f = 1;
for ( int i = 2; i <= n; i++)
f = f * i;
return f;
}
ll waysOfConsonants( int size1, int freq[])
{
ll ans = fact(size1);
for ( int i = 0; i < 26; i++) {
if (i == 0 || i == 4 || i == 8 || i == 14 || i == 20)
continue ;
else
ans = ans / fact(freq[i]);
}
return ans;
}
ll waysOfVowels( int size2, int freq[])
{
return fact(size2) / (fact(freq[0]) * fact(freq[4]) * fact(freq[8])
* fact(freq[14]) * fact(freq[20]));
}
ll countWays(string str)
{
int freq[26] = { 0 };
for ( int i = 0; i < str.length(); i++)
freq[str[i] - 'a' ]++;
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++;
}
return waysOfConsonants(consonant+1, freq) *
waysOfVowels(vowel, freq);
}
int main()
{
string str = "geeksforgeeks" ;
cout << countWays(str) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int fact( int n)
{
int f = 1 ;
for ( int i = 2 ; i <= n; i++)
f = f * i;
return f;
}
static int waysOfConsonants( int size1,
int []freq)
{
int ans = fact(size1);
for ( int i = 0 ; i < 26 ; i++)
{
if (i == 0 || i == 4 || i == 8 ||
i == 14 || i == 20 )
continue ;
else
ans = ans / fact(freq[i]);
}
return ans;
}
static int waysOfVowels( int size2, int [] freq)
{
return fact(size2) / (fact(freq[ 0 ]) *
fact(freq[ 4 ]) * fact(freq[ 8 ]) *
fact(freq[ 14 ]) * fact(freq[ 20 ]));
}
static 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' ]++;
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++;
}
return waysOfConsonants(consonant + 1 , freq) *
waysOfVowels(vowel, freq);
}
public static void main(String []args)
{
String str = "geeksforgeeks" ;
System.out.println(countWays(str));
}
}
|
Python3
def fact(n):
f = 1
for i in range ( 2 , n + 1 ):
f = f * i
return f
def waysOfConsonants(size1, freq):
ans = fact(size1)
for i in range ( 26 ):
if (i = = 0 or i = = 4 or
i = = 8 or i = = 14 or
i = = 20 ):
continue
else :
ans = ans / / fact(freq[i])
return ans
def waysOfVowels(size2, freq):
return (fact(size2) / / (fact(freq[ 0 ]) *
fact(freq[ 4 ]) * fact(freq[ 8 ]) *
fact(freq[ 14 ]) * fact(freq[ 20 ])))
def countWays(str1):
freq = [ 0 ] * 26
for i in range ( len (str1)):
freq[ ord (str1[i]) -
ord ( 'a' )] + = 1
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
return (waysOfConsonants(consonant + 1 , freq) *
waysOfVowels(vowel, freq))
if __name__ = = "__main__" :
str1 = "geeksforgeeks"
print (countWays(str1))
|
C#
using System.Collections.Generic;
using System;
class GFG{
static int fact( int n)
{
int f = 1;
for ( int i = 2; i <= n; i++)
f = f * i;
return f;
}
static int waysOfConsonants( int size1,
int []freq)
{
int ans = fact(size1);
for ( int i = 0; i < 26; i++)
{
if (i == 0 || i == 4 || i == 8 ||
i == 14 || i == 20)
continue ;
else
ans = ans / fact(freq[i]);
}
return ans;
}
static int waysOfVowels( int size2, int [] freq)
{
return fact(size2) / (fact(freq[0]) *
fact(freq[4]) * fact(freq[8]) *
fact(freq[14]) * fact(freq[20]));
}
static 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' ]++;
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++;
}
return waysOfConsonants(consonant + 1, freq) *
waysOfVowels(vowel, freq);
}
public static void Main()
{
string str = "geeksforgeeks" ;
Console.WriteLine(countWays(str));
}
}
|
Javascript
<script>
function fact(n)
{
let f = 1;
for (let i = 2; i <= n; i++)
f = f * i;
return f;
}
function waysOfConsonants(size1,freq)
{
let ans = fact(size1);
for (let i = 0; i < 26; i++)
{
if (i == 0 || i == 4 || i == 8 ||
i == 14 || i == 20)
continue ;
else
ans = Math.floor(ans / fact(freq[i]));
}
return ans;
}
function waysOfVowels(size2,freq)
{
return Math.floor(fact(size2) / (fact(freq[0]) *
fact(freq[4]) * fact(freq[8]) *
fact(freq[14]) * fact(freq[20])));
}
function 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)]++;
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++;
}
return waysOfConsonants(consonant + 1, freq) *
waysOfVowels(vowel, freq);
}
let str = "geeksforgeeks" ;
document.write(countWays(str));
</script>
|
Time complexity: O(n) where n is the length of the string
Auxiliary space: O(1)
Further Optimizations: We can pre-compute required factorial values to avoid re-computations.
Last Updated :
28 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...