给定一个整数N ,任务是计算由元音{a, e, i, o, u}组成的所有可能的长度为N 的字符串,这些元音可以形成为每个字符串按字典顺序排序。
例子:
Input: N = 2
Output: 15
Explanation: The strings of length 2 which are sorted in lexicographical order are [“aa”, “ae”, “ai”, “ao”, “au”, “ee”, “ei”, “eo”, “eu”, “ii”, “io”, “iu”, “oo”, “ou”, “uu”].
Input: N = 1
Output: 5
Explanation: The strings of length 1 which are sorted in lexicographical order are [“a”, “e”, “i”, “o”, “u”].
原始的方法:最简单的方法是生成的长度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++
#include
using namespace std;
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
int findNumberOfStrings(int N)
{
// Initializing vector to store count of strings.
vector counts(5, 1);
for (int i = 2; i <= N; i++) {
for (int j = 3; j >= 0; j--)
counts[j] += counts[j + 1];
}
int ans = 0;
// Summing up the total number of combinations.
for (auto c : counts)
ans += c;
// Return the result
return ans;
}
// Driver Code
int main()
{
int N = 2;
// Function Call
cout << findNumberOfStrings(N);
return 0;
}
// This code is contributed by Sarvesh Roshan.
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)
高效方法:上述方法可以进一步简化为线性时间和恒定空间。
以下是对不同长度字符串的一些观察:-
N | Number of strings starting with | Total Possible strings | ||||
‘a’ | ‘e’ | ‘i’ | ‘o’ | ‘u’ | ||
1 | 1 | 1 | 1 | 1 | 1 | 5 |
2 | 5 | 4 | 3 | 2 | 1 | 15 |
3 | 15 | 10 | 6 | 3 | 1 | 35 |
4 | 35 | 20 | 10 | 4 | 1 | 70 |
可以看出,对于N 的每个值,可能的字符串数量取决于N (N-1)的先前值。
第 N 行中任何列的值是第(N-1)行中所有列的总和,从右侧开始到该列号。
C++
#include
using namespace std;
// Function to count N-length strings
// consisting of vowels only sorted
// lexicographically
int findNumberOfStrings(int N)
{
// Initializing vector to store count of strings.
vector counts(5, 1);
for (int i = 2; i <= N; i++) {
for (int j = 3; j >= 0; j--)
counts[j] += counts[j + 1];
}
int ans = 0;
// Summing up the total number of combinations.
for (auto c : counts)
ans += c;
// Return the result
return ans;
}
// Driver Code
int main()
{
int N = 2;
// Function Call
cout << findNumberOfStrings(N);
return 0;
}
// This code is contributed by Sarvesh Roshan.
15
时间复杂度: O(5*N)
空间复杂度: O(1)
高效方法:上述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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。