Number of words that can be made using exactly P consonants and Q vowels from the given string | C ++ implementation of the approach ; Function to return the value of nCk ; Function to return the factorial of n ; Function that returns true if ch is a vowel ; Function to return the number of words possible ; To store the count of vowels and consonanats in the given string ; If current character is a vowel ; Find the total possible words ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE lli binomialCoeff ( lli n , lli k ) { if ( k == 0 k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } lli fact ( lli n ) { if ( n >= 1 ) return n * fact ( n - 1 ) ; else return 1 ; } bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) { return true ; } return false ; } lli countWords ( string s , int p , int q ) { lli countc = 0 , countv = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s [ i ] ) ) countv ++ ; else countc ++ ; } lli a = binomialCoeff ( countc , p ) ; lli b = binomialCoeff ( countv , q ) ; lli c = fact ( p + q ) ; lli ans = ( a * b ) * c ; return ans ; } int main ( ) { string s = " crackathon " ; int p = 4 , q = 3 ; cout << countWords ( s , p , q ) ; return 0 ; }