📌  相关文章
📜  计算仅由按字典顺序排序的元音组成的 N 长度字符串

📅  最后修改于: 2021-09-04 08:10:23             🧑  作者: Mango

给定一个整数N ,任务是计算由元音{a, e, i, o, u}组成的所有可能的长度为N 的字符串,这些元音可以形成为每个字符串按字典顺序排序。

例子:

原始的方法:最简单的方法是生成的长度N的所有可能的字符串仅由元音和计数只有那些在词典编纂顺序排序字符串。打印完成步骤后获得的计数。
时间复杂度: O(N*N!)
辅助空间: O(1)

高效的方法:为了优化上述方法,思想是使用动态规划。以下是解决给定问题的一些观察结果:

  • 从字符a、e、i、o 和 u开始的长度为1的字典序排序字符串的计数为1
  • 按字典顺序从字符a、e、i、o 和 u开始的长度为2的字符串的计数由下式给出:
    • 从字符a开始的长度为2的字典排序字符串的计数由长度为1的字典字符串的计数给出,该字符串从大于或等于a 的字符开始。因此,计数为5
    • 从字符e开始的按字典顺序排序的长度为2 的字符串的计数由从大于或等于e 的字符开始的长度为1的字典字符串的计数给出。因此,计数为4
    • 从字符i开始的长度为2的字典序排序字符串的计数由长度为1的字典序字符串的计数给出,该字符串从大于或等于i 的字符开始。因此,计数为3
    • 从字符o开始的长度为2的字典序排序字符串的计数由长度为1的字典序字符串的计数给出,该字符串从大于或等于o 的字符开始。因此,计数为2
    • 从字符u开始的长度为2的字典序排序字符串的计数由长度为1的字典序字符串的计数给出,该字符串从大于或等于u 的字符开始。因此,计数为1
  • 因此,长度为2的字符串总数为: 5 + 4 + 3 + 2 + 1 = 15
  • 通过观察上述模式,从每个元音字符ch开始的长度为N的字符串的计数由长度为(N – 1)的字典序字符串的计数总和给出,该字符串从大于或等于ch 的字符开始。

请按照以下步骤解决问题:

  • 创建一个二维数组, dp[N+1][6] ,其中dp[i][j]表示可以使用前j个元音构造的长度为i的字典序排序字符串的数量,并初始化dp[1][1]1
  • 使用变量j迭代第一行设置dp[1][j] = dp[1][j – 1] + 1因为长度为1的字符串总是按字典顺序排序。
  • 遍历二维数组dp[][]并将每个 dp 状态更新为dp[i][j] = dp[i][j – 1] + dp[i – 1][j] ,其中dp[i][j] – 1]将给出字典字符串长度N的计数, dp[i – 1][j]将给出字典字符串长度(N – 1)的计数。
  • 完成上述步骤后,打印dp[N][5] 的值作为结果字符串的总数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
int findNumberOfStrings(int n)
{
    // Stores count of strings consisting
    // of vowels sorted lexiographically
    // of all possible lengths
    vector > DP(n + 1,
                            vector(6));
 
    // Initialize DP[1][1]
    DP[1][1] = 1;
 
    // Traverse the matrix row-wise
    for (int i = 1; i < n + 1; i++) {
 
        for (int j = 1; j < 6; j++) {
 
            // Base Case
            if (i == 1) {
                DP[i][j] = DP[i][j - 1] + 1;
            }
 
            else {
                DP[i][j] = DP[i][j - 1]
                           + DP[i - 1][j];
            }
        }
    }
 
    // Return the result
    return DP[n][5];
}
 
// Driver Code
int main()
{
    int N = 2;
 
    // Function Call
    cout << findNumberOfStrings(N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
   
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
static int findNumberOfStrings(int n)
{
  // Stores count of strings consisting
  // of vowels sorted lexiographically
  // of all possible lengths
  int DP[][] = new int [n + 1][6];
 
  // Initialize DP[1][1]
  DP[1][1] = 1;
 
  // Traverse the matrix row-wise
  for (int i = 1; i < n + 1; i++)
  {
    for (int j = 1; j < 6; j++)
    {
      // Base Case
      if (i == 1)
      {
        DP[i][j] = DP[i][j - 1] + 1;
      }
 
      else
      {
        DP[i][j] = DP[i][j - 1] +
                   DP[i - 1][j];
      }
    }
  }
 
  // Return the result
  return DP[n][5];
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 2;
 
  // Function Call
  System.out.print(findNumberOfStrings(N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the
# above approach
 
# Function to count N-length
# strings consisting of vowels
# only sorted lexicographically
def findNumberOfStrings(n):
   
    # Stores count of strings
    # consisting of vowels
    # sorted lexiographically
    # of all possible lengths
    DP = [[0 for i in range(6)]
             for i in range(n + 1)]
 
    # Initialize DP[1][1]
    DP[1][1] = 1
 
    # Traverse the matrix row-wise
    for i in range(1, n + 1):
        for j in range(1, 6):
 
            #Base Case
            if (i == 1):
                DP[i][j] = DP[i][j - 1] + 1
            else:
                DP[i][j] = DP[i][j - 1]+ DP[i - 1][j]
 
    # Return the result
    return DP[n][5]
 
# Driver Code
if __name__ == '__main__':
   
    N = 2
 
    # Function Call
    print(findNumberOfStrings(N))
 
# This code is contributed by Mohit Kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
   
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
static int findNumberOfStrings(int n)
{
  // Stores count of strings consisting
  // of vowels sorted lexiographically
  // of all possible lengths
  int[,] DP = new int [n + 1, 6];
 
  // Initialize DP[1][1]
  DP[1, 1] = 1;
 
  // Traverse the matrix row-wise
  for (int i = 1; i < n + 1; i++)
  {
    for (int j = 1; j < 6; j++)
    {
      // Base Case
      if (i == 1)
      {
        DP[i, j] = DP[i, j - 1] + 1;
      }
 
      else
      {
        DP[i, j] = DP[i, j - 1] +
                   DP[i - 1, j];
      }
    }
  }
 
  // Return the result
  return DP[n, 5];
}
 
// Driver Code
public static void Main(string[] args)
{
  int N = 2;
 
  // Function Call
  Console.Write(findNumberOfStrings(N));
}
}
 
// This code is contributed by Chitranayal


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
int findNumberOfStrings(int n)
{
    return (n+1)*(n+2)*(n+3)*(n+4)/24;
}
 
// Driver Code
int main()
{
    int N = 2;
 
    // Function Call
    cout << findNumberOfStrings(N);
 
    return 0;
}
// This code is contributed by Kartik Singh


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
   
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
static int findNumberOfStrings(int n)
{
 return (n+1)*(n+2)*(n+3)*(n+4)/24;
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 2;
 
  // Function Call
  System.out.print(findNumberOfStrings(N));
}
}
 
// This code is contributed by Kartik Singh


Python
# Python3 program for the
# above approach
 
# Function to count N-length
# strings consisting of vowels
# only sorted lexicographically
def findNumberOfStrings(n):
    return int((n+1)*(n+2)*(n+3)*(n+4)/24)
 
# Driver Code
if __name__ == '__main__':
   
    N = 2
 
    # Function Call
    print(findNumberOfStrings(N))
 
# This code is contributed by Kartik Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
   
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
static int findNumberOfStrings(int n)
{
  return (n+1)*(n+2)*(n+3)*(n+4)/24;
}
 
// Driver Code
public static void Main(string[] args)
{
  int N = 2;
 
  // Function Call
  Console.Write(findNumberOfStrings(N));
}
}
 
// This code is contributed by Kartik Singh


Javascript


输出
15

时间复杂度: O(N*5)
辅助空间: O(N*5)

高效方法:上述dp方法的相同思想可以在恒定时间恒定空间中实现

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
int findNumberOfStrings(int n)
{
    return (n+1)*(n+2)*(n+3)*(n+4)/24;
}
 
// Driver Code
int main()
{
    int N = 2;
 
    // Function Call
    cout << findNumberOfStrings(N);
 
    return 0;
}
// This code is contributed by Kartik Singh

Java

// Java program to implement
// the above approach
import java.util.*;
class GFG{
   
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
static int findNumberOfStrings(int n)
{
 return (n+1)*(n+2)*(n+3)*(n+4)/24;
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 2;
 
  // Function Call
  System.out.print(findNumberOfStrings(N));
}
}
 
// This code is contributed by Kartik Singh

Python

# Python3 program for the
# above approach
 
# Function to count N-length
# strings consisting of vowels
# only sorted lexicographically
def findNumberOfStrings(n):
    return int((n+1)*(n+2)*(n+3)*(n+4)/24)
 
# Driver Code
if __name__ == '__main__':
   
    N = 2
 
    # Function Call
    print(findNumberOfStrings(N))
 
# This code is contributed by Kartik Singh

C#

// C# program to implement
// the above approach
using System;
class GFG{
   
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
static int findNumberOfStrings(int n)
{
  return (n+1)*(n+2)*(n+3)*(n+4)/24;
}
 
// Driver Code
public static void Main(string[] args)
{
  int N = 2;
 
  // Function Call
  Console.Write(findNumberOfStrings(N));
}
}
 
// This code is contributed by Kartik Singh

Javascript


输出
15

时间复杂度:O(1)

辅助空间:O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live