Number of words that can be made using exactly P consonants and Q vowels from the given string
Last Updated :
21 Jun, 2022
Given a string str and two integers P and Q. The task is to find the total count of words that can be formed by choosing exactly P consonants and Q vowels from the given string.
Examples:
Input: str = “geek”, P = 1, Q = 1
Output: 8
“ge”, “ge”, “eg”, “ek”, “eg”, “ek”,
“ke” and “ke” are the possible words.
Input: str = “crackathon”, P = 4, Q = 3
Output: 176400
Approach: Since P consonants and Q vowels has to be chosen from the original count of consonants and vowels in the given string. So, binomial coefficient can be used to calculate the combinations of choosing these characters and the characters chosen can be arranged in themselves using the factorial of their count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
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;
}
|
Java
class GFG
{
static long binomialCoeff( long n, long k)
{
if (k == 0 || k == n)
return 1 ;
return binomialCoeff(n - 1 , k - 1 ) +
binomialCoeff(n - 1 , k);
}
static long fact( long n)
{
if (n >= 1 )
return n * fact(n - 1 );
else
return 1 ;
}
static boolean isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' )
{
return true ;
}
return false ;
}
static long countWords(String s, int p, int q)
{
long countc = 0 , countv = 0 ;
for ( int i = 0 ; i < s.length(); i++)
{
if (isVowel(s.charAt(i)))
countv++;
else
countc++;
}
long a = binomialCoeff(countc, p);
long b = binomialCoeff(countv, q);
long c = fact(p + q);
long ans = (a * b) * c;
return ans;
}
public static void main (String[] args)
{
String s = "crackathon" ;
int p = 4 , q = 3 ;
System.out.println(countWords(s, p, q));
}
}
|
Python3
def binomialCoeff(n, k):
if (k = = 0 or k = = n):
return 1
return binomialCoeff(n - 1 , k - 1 ) + \
binomialCoeff(n - 1 , k)
def fact(n):
if (n > = 1 ):
return n * fact(n - 1 )
else :
return 1
def isVowel(ch):
if (ch = = 'a' or ch = = 'e' or
ch = = 'i' or ch = = 'o' or ch = = 'u' ):
return True
return False
def countWords(s, p, q):
countc = 0
countv = 0
for i in range ( len (s)):
if (isVowel(s[i])):
countv + = 1
else :
countc + = 1
a = binomialCoeff(countc, p)
b = binomialCoeff(countv, q)
c = fact(p + q)
ans = (a * b) * c
return ans
s = "crackathon"
p = 4
q = 3
print (countWords(s, p, q))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static long binomialCoeff( long n, long k)
{
if (k == 0 || k == n)
return 1;
return binomialCoeff(n - 1, k - 1) +
binomialCoeff(n - 1, k);
}
static long fact( long n)
{
if (n >= 1)
return n * fact(n - 1);
else
return 1;
}
static bool isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' )
{
return true ;
}
return false ;
}
static long countWords(String s, int p, int q)
{
long countc = 0, countv = 0;
for ( int i = 0; i < s.Length; i++)
{
if (isVowel(s[i]))
countv++;
else
countc++;
}
long a = binomialCoeff(countc, p);
long b = binomialCoeff(countv, q);
long c = fact(p + q);
long ans = (a * b) * c;
return ans;
}
public static void Main (String[] args)
{
String s = "crackathon" ;
int p = 4, q = 3;
Console.WriteLine(countWords(s, p, q));
}
}
|
Javascript
<script>
function binomialCoeff(n, k)
{
if (k == 0 || k == n)
return 1;
return binomialCoeff(n - 1, k - 1)
+ binomialCoeff(n - 1, k);
}
function fact(n)
{
if (n >= 1)
return n * fact(n - 1);
else
return 1;
}
function isVowel(ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u' ) {
return true ;
}
return false ;
}
function countWords(s, p, q)
{
var countc = 0, countv = 0;
for ( var i = 0; i < s.length; i++) {
if (isVowel(s[i]))
countv++;
else
countc++;
}
var a = binomialCoeff(countc, p);
var b = binomialCoeff(countv, q);
var c = fact(p + q);
var ans = (a * b) * c;
return ans;
}
var s = "crackathon" ;
var p = 4, q = 3;
document.write(countWords(s, p, q));
</script>
|
Time Complexity: O(2n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...