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++
// 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^exponentunsigned 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 stringsunsigned 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 Programint 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 consonantclass GFG{Â
static int mod = (int) (1e9 + 7);Â
// Function to return base^exponentstatic 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 Stringsstatic 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 codepublic 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 consonantmod = 1e9 + 7Â
# Function to return base^exponentdef 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 stringsdef 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 Programif __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 consonantusing System;Â
class GFG{Â
static int mod = (int)(1e9 + 7);Â
// Function to return base^exponentstatic 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 Stringsstatic 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 codepublic 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> |
8190
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



