Count of N size strings consisting of at least one vowel and one consonant
Last Updated :
08 Mar, 2022
Given an integer N, which represents the length of a string, the task is to count the number of strings possible of length N which consists of only one vowel and one consonant.
Note: Since the output can be large print in modulo 1000000007
Examples:
Input: N = 2
Output: 210
Explanation:
There are 5 vowels and 21 consonants in English alphabets.
So for vowel ‘a’ we can have 42 strings of the form ‘ab’, ‘ba’, ‘ac’, ‘ca’, ‘ad’, ‘da’ and so on.
For the other 4 vowels, the same process repeats, and we get a total of 210 such strings.
Input: N = 3
Output: 8190
Approach:
To solve the problem mentioned above, we need to ignore the strings that comprise only vowels(to allow at least one consonant) and only consonants(to allow at least one vowel). Hence, the required answer is:
All N length strings possible – (N length strings consisting of only vowels + N length strings consisting of only consonants) = 26 N – (5 N + 21 N)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const unsigned long long mod = 1e9 + 7;
unsigned long long expo(
unsigned long long base,
unsigned long long exponent)
{
unsigned long long ans = 1;
while (exponent != 0) {
if ((exponent & 1) == 1) {
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1;
}
return ans % mod;
}
unsigned long long findCount(
unsigned long long N)
{
unsigned long long ans
= (expo(26, N)
- expo(5, N)
- expo(21, N))
% mod;
ans += mod;
ans %= mod;
return ans;
}
int main()
{
unsigned long long N = 3;
cout << findCount(N);
return 0;
}
|
Java
class GFG{
static int mod = ( int ) (1e9 + 7 );
static int expo( int base, int exponent)
{
int ans = 1 ;
while (exponent != 0 )
{
if ((exponent & 1 ) == 1 )
{
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1 ;
}
return ans % mod;
}
static int findCount( int N)
{
int ans = (expo( 26 , N) -
expo( 5 , N) -
expo( 21 , N))% mod;
ans += mod;
ans %= mod;
return ans;
}
public static void main(String[] args)
{
int N = 3 ;
System.out.print(findCount(N));
}
}
|
Python3
mod = 1e9 + 7
def expo(base, exponent):
ans = 1
while (exponent ! = 0 ):
if ((exponent & 1 ) = = 1 ):
ans = ans * base
ans = ans % mod
base = base * base
base % = mod
exponent >> = 1
return ans % mod
def findCount(N):
ans = ((expo( 26 , N) -
expo( 5 , N) -
expo( 21 , N)) %
mod)
ans + = mod
ans % = mod
return ans
if __name__ = = "__main__" :
N = 3
print ( int (findCount(N)))
|
C#
using System;
class GFG{
static int mod = ( int )(1e9 + 7);
static int expo( int Base, int exponent)
{
int ans = 1;
while (exponent != 0)
{
if ((exponent & 1) == 1)
{
ans = ans * Base;
ans = ans % mod;
}
Base = Base * Base;
Base %= mod;
exponent >>= 1;
}
return ans % mod;
}
static int findCount( int N)
{
int ans = (expo(26, N) -
expo(5, N) -
expo(21, N)) % mod;
ans += mod;
ans %= mod;
return ans;
}
public static void Main(String[] args)
{
int N = 3;
Console.Write(findCount(N));
}
}
|
Javascript
<script>
var mod = parseInt( 1e9 + 7);
function expo(base , exponent) {
var ans = 1;
while (exponent != 0) {
if ((exponent & 1) == 1) {
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1;
}
return ans % mod;
}
function findCount(N) {
var ans = (expo(26, N) -
expo(5, N) -
expo(21, N)) % mod;
ans += mod;
ans %= mod;
return ans;
}
var N = 3;
document.write(findCount(N));
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...