📌  相关文章
📜  由至少一个元音和一个辅音组成的N个大小的字符串的数量

📅  最后修改于: 2021-05-06 18:06:55             🧑  作者: Mango

给定一个代表字符串长度的整数N ,任务是计算长度为N的可能字符串的数量,该长度仅由一个元音和一个辅音组成。
注意:由于输出可以以模1000000007为大字体
例子:

方法:
为了解决上述问题,我们需要忽略仅包含元音(允许至少一个辅音)和仅包含辅音(允许至少一个元音)的字符串。因此,所需的答案是:

下面是上述方法的实现:

C++
// C++ program to count all
// possible strings of length N
// consisting of atleast one
// vowel and one consonant
#include 
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


输出:
8190