Count all possible Nlength vowel permutations that can be generated based on the given conditions (original) (raw)

Last Updated : 23 Jul, 2025

Given an integer **N, the task is to count the number of N-length strings consisting of lowercase vowels that can be generated based the following conditions:

**Examples:

**Input: N = 1
**Output: 5
**Explanation: All strings that can be formed are: "a", "e", "i", "o" and "u".

**Input: N = 2
**Output: 10
**Explanation: All strings that can be formed are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".

**Approach: The idea to solve this problem is to visualize this as a Graph Problem. From the given rules a directed graph can be constructed, where an edge from **u to **v means that **v can be immediately written after **u in the resultant strings. The problem reduces to finding the number of N-length paths in the constructed directed graph. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++ `

// C++ program for the above approach #include <bits/stdc++.h> using namespace std;

// Function to find the number of // vowel permutations possible int countVowelPermutation(int n) {

// To avoid the large output value
int MOD = (int)(1e9 + 7);

// Initialize 2D dp array
long dp[n + 1][5];

// Initialize dp[1][i] as 1 since
// string of length 1 will consist
// of only one vowel in the string
for(int i = 0; i < 5; i++)
{
    dp[1][i] = 1;
}

// Directed graph using the
// adjacency matrix
vector<vector<int>> relation = {
    { 1 }, { 0, 2 },
    { 0, 1, 3, 4 },
    { 2, 4 }, { 0 }
};

// Iterate over the range [1, N]
for(int i = 1; i < n; i++) 
{
    
    // Traverse the directed graph
    for(int u = 0; u < 5; u++)
    {
        dp[i + 1][u] = 0;

        // Traversing the list
        for(int v : relation[u])
        {
            
            // Update dp[i + 1][u]
            dp[i + 1][u] += dp[i][v] % MOD;
        }
    }
}

// Stores total count of permutations
long ans = 0;

for(int i = 0; i < 5; i++) 
{
    ans = (ans + dp[n][i]) % MOD;
}

// Return count of permutations
return (int)ans;

}

// Driver code int main() { int N = 2;

cout << countVowelPermutation(N);

}

// This code is contributed by Mohit kumar 29

Java

// Java program for the above approach

import java.io.; import java.util.; class GFG {

// Function to find the number of
// vowel permutations possible
public static int
countVowelPermutation(int n)
{
    // To avoid the large output value
    int MOD = (int)(1e9 + 7);

    // Initialize 2D dp array
    long[][] dp = new long[n + 1][5];

    // Initialize dp[1][i] as 1 since
    // string of length 1 will consist
    // of only one vowel in the string
    for (int i = 0; i < 5; i++) {
        dp[1][i] = 1;
    }

    // Directed graph using the
    // adjacency matrix
    int[][] relation = new int[][] {
        { 1 }, { 0, 2 }, 
        { 0, 1, 3, 4 }, 
        { 2, 4 }, { 0 }
    };

    // Iterate over the range [1, N]
    for (int i = 1; i < n; i++) {

        // Traverse the directed graph
        for (int u = 0; u < 5; u++) {
            dp[i + 1][u] = 0;

            // Traversing the list
            for (int v : relation[u]) {

                // Update dp[i + 1][u]
                dp[i + 1][u] += dp[i][v] % MOD;
            }
        }
    }

    // Stores total count of permutations
    long ans = 0;

    for (int i = 0; i < 5; i++) {
        ans = (ans + dp[n][i]) % MOD;
    }

    // Return count of permutations
    return (int)ans;
}

// Driver Code
public static void main(String[] args)
{
    int N = 2;
    System.out.println(
        countVowelPermutation(N));
}

}

Python3

Python 3 program for the above approach

Function to find the number of

vowel permutations possible

def countVowelPermutation(n):

# To avoid the large output value
MOD =  1e9 + 7

# Initialize 2D dp array
dp = [[0 for i in range(5)] for j in range(n + 1)]

# Initialize dp[1][i] as 1 since
# string of length 1 will consist
# of only one vowel in the string
for i in range(5):
    dp[1][i] = 1

# Directed graph using the
# adjacency matrix
relation = [[1],[0, 2], [0, 1, 3, 4], [2, 4],[0]]

# Iterate over the range [1, N]
for i in range(1, n, 1):
  
    # Traverse the directed graph
    for u in range(5):
        dp[i + 1][u] = 0

        # Traversing the list
        for v in relation[u]:
          
            # Update dp[i + 1][u]
            dp[i + 1][u] += dp[i][v] % MOD

# Stores total count of permutations
ans = 0
for i in range(5):
    ans = (ans + dp[n][i]) % MOD

# Return count of permutations
return int(ans)

Driver code

if name == 'main': N = 2 print(countVowelPermutation(N))

# This code is contributed by bgangwar59.

C#

// C# program to find absolute difference // between the sum of all odd frequency and // even frequent elements in an array using System; using System.Collections.Generic; class GFG {

// Function to find the number of
// vowel permutations possible
static int countVowelPermutation(int n)
{
  
    // To avoid the large output value
    int MOD = (int)(1e9 + 7);

    // Initialize 2D dp array
    long[,] dp = new long[n + 1, 5];

    // Initialize dp[1][i] as 1 since
    // string of length 1 will consist
    // of only one vowel in the string
    for (int i = 0; i < 5; i++) {
        dp[1, i] = 1;
    }

    // Directed graph using the
    // adjacency matrix
    List<List<int>> relation = new List<List<int>>();
    relation.Add(new List<int> { 1 });
    relation.Add(new List<int> { 0, 2 });
    relation.Add(new List<int> { 0, 1, 3, 4 });
    relation.Add(new List<int> { 2, 4 });
    relation.Add(new List<int> { 0 });

    // Iterate over the range [1, N]
    for (int i = 1; i < n; i++) 
    {

        // Traverse the directed graph
        for (int u = 0; u < 5; u++)
        {
            dp[i + 1, u] = 0;

            // Traversing the list
            foreach(int v in relation[u]) 
            {

                // Update dp[i + 1][u]
                dp[i + 1, u] += dp[i, v] % MOD;
            }
        }
    }

    // Stores total count of permutations
    long ans = 0;

    for (int i = 0; i < 5; i++)
    {
        ans = (ans + dp[n, i]) % MOD;
    }

    // Return count of permutations
    return (int)ans;
} 

// Driver code static void Main() { int N = 2; Console.WriteLine(countVowelPermutation(N)); } }

// This code is contributed by divyesh072019.

JavaScript

`

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

**Efficient Approach : As we see that we only need current and previous state of the dp array, We can definitely space optimize the above solution. We can have 5 variables of previous state and 5 variables of current state that we need to compute with the given mapping relations.

**Below is the implementation of above approach.

C++ `

// C++ program for the above approach #include <bits/stdc++.h> using namespace std;

// Function to find the number of // vowel permutations possible int countVowelPermutation(int n) { const int MOD = 1e9 + 7; // initialize current and previous state // variables. long a = 1, e = 1, i = 1, o = 1, u = 1, a_new, e_new, i_new, o_new, u_new;

for(int j = 2; j <= n; j++) {
  // respective conditions
  a_new =  e;
  e_new = (a + i) % MOD;
  i_new = (a + e + o + u) % MOD;
  o_new = (i + u) % MOD;
  u_new =  a;
  // make current computed states
  // to make them previous for next computing
  // future states
  a = a_new, e = e_new, i = i_new, o = o_new, u = u_new;
}
  // return the answer
return (a + e + i + o + u) % MOD;

}

// Driver code int main() { int N = 2;

cout << countVowelPermutation(N);

}

Java

public class VowelPermutation {

static int countVowelPermutation(int n)
{
    // Define the modulo value
    final int MOD = 1000000007;

    // Initialize current and previous state variables
    int a = 1, e = 1, i = 1, o = 1, u = 1;

    for (int j = 2; j <= n; j++) {
        // Calculate new states based on conditions
        int aNew = e;
        int eNew = (a + i) % MOD;
        int iNew = (a + e + o + u) % MOD;
        int oNew = (i + u) % MOD;
        int uNew = a;

        // Update current states for the next
        // computation
        a = aNew;
        e = eNew;
        i = iNew;
        o = oNew;
        u = uNew;
    }

    // Return the result
    return (a + e + i + o + u) % MOD;
}

public static void main(String[] args)
{
    // Driver code
    int N = 2;
    System.out.println(countVowelPermutation(N));
}

}

Python3

def countVowelPermutation(n): MOD = 10 ** 9 + 7 # Initialize current and previous state variables. a = e = i = o = u = 1 for j in range(2, n + 1): # Respective conditions a_new = e e_new = (a + i) % MOD i_new = (a + e + o + u) % MOD o_new = (i + u) % MOD u_new = a # Update current states for next computation a, e, i, o, u = a_new, e_new, i_new, o_new, u_new # Return the answer return (a + e + i + o + u) % MOD

Driver code

N = 2 print(countVowelPermutation(N))

C#

using System;

class Program { // Function to find the number of vowel permutations // possible static int CountVowelPermutation(int n) { const int MOD = 1000000007; // Initialize current and previous state variables. long a = 1, e = 1, i = 1, o = 1, u = 1, a_new, e_new, i_new, o_new, u_new;

    for (int j = 2; j <= n; j++) {
        // Respective conditions
        a_new = e;
        e_new = (a + i) % MOD;
        i_new = (a + e + o + u) % MOD;
        o_new = (i + u) % MOD;
        u_new = a;

        // Make current computed states
        // to make them previous for next computing
        // future states
        a = a_new;
        e = e_new;
        i = i_new;
        o = o_new;
        u = u_new;
    }
    // Return the answer
    return (int)((a + e + i + o + u) % MOD);
}

// Driver code
static void Main()
{
    int N = 2;
    Console.WriteLine(CountVowelPermutation(N));
}

}

JavaScript

function countVowelPermutation(n) { const MOD = 10 ** 9 + 7; // Initialize current and previous state variables. let a = e = i = o = u = 1; for (let j = 2; j <= n; j++) { // Respective conditions const a_new = e; const e_new = (a + i) % MOD; const i_new = (a + e + o + u) % MOD; const o_new = (i + u) % MOD; const u_new = a; // Update current states for next computation a = a_new; e = e_new; i = i_new; o = o_new; u = u_new; } // Return the answer return (a + e + i + o + u) % MOD; }

// Driver code const N = 2; console.log(countVowelPermutation(N));

`

**Time Complexity : O(N)

**Space Complexity : O(1)