Open In App

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

Last Updated : 08 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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;
}


Java




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


Python3




# 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


C#




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


Javascript




<script>
 
// Javascript program to count all
// possible Strings of length N
// consisting of atleast one
// vowel and one consonant   
var mod = parseInt( 1e9 + 7);
 
    // Function to return base^exponent
    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 to count all possible Strings
    function findCount(N) {
 
        // All possible Strings of length N
        var 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
     
        var N = 3;
        document.write(findCount(N));
 
// This code is contributed by todaysgaurav
 
</script>


Output

8190

Time Complexity: O(log10N)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads