Count of N size strings consisting of at least one vowel and one consonant

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count all
// possible strings of length N
// consisting of atleast one
// vowel and one consonant
#include <bits/stdc++.h>
using namespace std;
 
const unsigned long long mod = 1e9 + 7;
 
// Function to return base^exponent
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;
}
 
// Function to count all possible strings
unsigned long long findCount(
    unsigned long long N)
{
    // All possible strings of length N
    unsigned long long ans
        = (expo(26, N)
 
           // vowels only
           - expo(5, N)
 
           // consonants only
           - expo(21, N))
 
          % mod;
 
    ans += mod;
    ans %= mod;
 
    // Return the
    // final result
    return ans;
}
 
// Driver Program
int main()
{
    unsigned long long N = 3;
    cout << findCount(N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count all
// possible Strings of length N
// consisting of atleast one
// vowel and one consonant
class GFG{
 
static int mod = (int) (1e9 + 7);
 
// Function to return base^exponent
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;
}
 
// Function to count all possible Strings
static int findCount(int N)
{
     
    // All possible Strings of length N
    int ans = (expo(26, N) -
               
               // Vowels only
               expo(5, N) -
                
               // Consonants only
               expo(21, N))% mod;
    ans += mod;
    ans %= mod;
 
    // Return the
    // final result
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 3;
    System.out.print(findCount(N));
}
}
 
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to count all
# possible strings of length N
# consisting of atleast one
# vowel and one consonant
mod = 1e9 + 7
 
# Function to return base^exponent
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
 
# Function to count all
# possible strings
def findCount(N):
 
    # All possible strings
    # of length N
    ans = ((expo(26, N) -
             
            # vowels only
            expo(5, N) -
 
            # consonants only
            expo(21, N)) %
            mod)
 
    ans += mod
    ans %= mod
 
    # Return the
    # final result
    return ans
 
# Driver Program
if __name__ == "__main__":
    N = 3
    print (int(findCount(N)))
 
# This code is contributed by Chitranayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count all possible Strings
// of length N consisting of atleast one
// vowel and one consonant
using System;
 
class GFG{
 
static int mod = (int)(1e9 + 7);
 
// Function to return base^exponent
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;
}
 
// Function to count all possible Strings
static int findCount(int N)
{
     
    // All possible Strings of length N
    int ans = (expo(26, N) -
                
               // Vowels only
               expo(5, N) -
                
               // Consonants only
               expo(21, N)) % mod;
    ans += mod;
    ans %= mod;
 
    // Return the
    // readonly result
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 3;
     
    Console.Write(findCount(N));
}
}
 
// This code is contributed by Rajput-Ji

chevron_right


Output: 

8190

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, chitranayal