CMEET - Editorial

PROBLEM LINK:

Practice
Contest

Author: Vikas Yadav
Tester: Keshav Kumar
Editorialist: Vikas Yadav

DIFFICULTY:

EASY

PREREQUISITES:

Factorial
Permutation

PROBLEM:

Find the number of ways of arranging the letters of given string such that all the vowels always come together.

QUICK EXPLANATION:

Count the number of Vowels and Consonants in the given string. If there is no vowel in the string then only calculate the factorial of number of consonants. Otherwise multiply the factorial of number of vowels and factorial of (number of consonants + 1).

EXPLANATION:

Let say length of the string is N. and there are C consonants & V vowels in the string.
When the vowels are always together, they can be supposed to form 1 letter.
Then, we have to arrange the consonants letters. Now because we have supposed that all vowels form 1 letter and there are C consonants totally they form
(C+1)! different strings now vowels can be arrange among themselves in V! ways.
So there will be (C+1)!*V! ways.

Example:

Let’s assume string is LEADING.
It has 7 different letters.
When the vowels E A I are always together, they can be supposed as one letter.
Then, we have to arrange the letters L N D G (EAI).
Now, (4 + 1) = 5 letters can be arranged in 5! = 120 ways.
The vowels (EAI) can be arranged among themselves in 3! = 6 ways.
Required number of ways: (120 * 6) = 720
For more details check Setter’s Solution.

SOLUTIONS:

Setter's Solution
#include <stdio.h>

//Custom function which counts number of vowels in a given string
int isVowel(char ch)
{
    char vowels[5] = "AEIOU";
	for(int i = 0; i < 5; i++)
	{
	    if(ch == vowels[i])
			return 1;
    }
    return 0;
}

//Factorial function
unsigned long long factorial(int n)
{
	if(n == 0 || n == 1)
		return 1;
	else
		return n * factorial(n-1);
}


int main(void) {
	
	int test;
	scanf("%d", &test);
	
	while(test--)
	{
	    int size, vowel_count = 0, consonant_count, i;
	    scanf("%d", &size);
	    
	    char str[size+1];
	    scanf("%s", str);
	    
	    for(i = 0; i < size; i++)
	        if(isVowel(str[i]))
	            vowel_count++;
	            
	    consonant_count = size - vowel_count;
	    
	    unsigned long long total_ways;
	    
	    if(vowel_count)
	        total_ways = factorial(vowel_count) * factorial(consonant_count + 1);
	    else
	        total_ways = factorial(consonant_count);
	   
	    
	    printf("%llu\n", total_ways);
	    
	}
	
	return 0;
}