Count N-length strings consisting only of vowels sorted lexicographically

Given an integer N, the task is to count all possible strings of length N consisting of vowels {a, e, i, o, u} that can be formed such that each string is sorted in lexicographical order.

Examples:

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”].

Naive Approach: The simplest approach is to generate all possible strings of length N consisting only of vowels and count only those string that is sorted in lexicographical order. Print the count obtained after completing the steps. 
Time Complexity: O(N*N!)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming. Below are some observations to solve the given problem:



Follow the steps below to solve the problem:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
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<vector<int> > DP(n + 1,
                            vector<int>(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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

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

Output: 
15








 

Time Complexity: O(N*5)
Auxiliary Space: O(N*5)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Article Tags :