Friday, September 27, 2024
Google search engine
HomeData Modelling & AICount ways to create string of size N with given restrictions

Count ways to create string of size N with given restrictions

Given a number N, the task is to count the number of ways to create a string of size N (only with capital alphabets) such that no vowel is between two consonants and the string does not start with the letter ā€˜Aā€™ and does not end with the letter ā€˜Zā€™. (Print the answer modulo 109 + 7).

Examples:

Input: N =Ā 1
Output: 24
Explanation: There are 24 ways of filling 1 sized string by every character of the alphabet apart from the letter ā€˜Aā€™ because it cannot be the first letter of the required string and the letter ā€˜Zā€™ because it cannot be the last letter of required string and for 1 sized string first and last positions are same.

Input: N =Ā 2
Output: 625
Explanation: The first position can be filled in 25 ways(because letter A cannot be on that position as it is the first position so one less than 26) and the second position can be filled in 25 ways(because letter Z cannot be on that position as it is the last position so one less than 26), Therefore, Total ways = 25 * 25 = 625

Naive approach: The basic way to solve the problem is as follows:

The basic way to solve this problem is to generate all possible combinations by using a recursive approach.

Time Complexity: O(6N)
Auxiliary Space: O(1)

Efficient Approach: Ā The above approach can be optimized based on the following idea:

Bitmask Dynamic programming can be used to solve this problem

  • Bitmask of the last two characters is maintained in order to avoid choosing a vowel in between two consonants.
  • dp[i][j] represents the number of ways of creating a string of size i Ā and j is the bitmask for the last two characters.

It can be observed that the recursive function is called exponential times. That means that some states are called repeatedly. So the idea is to store the value of each state. This can be done using by the store the value of a state and whenever the function is called, returning the stored value without computing again.

Follow the steps below to solve the problem:

  • Create a recursive function that takes two parameters representing the ith position to be filled and j representing the bitmask of the last two characters.
  • Call the recursive function for choosing all characters from 1 to 26.
  • Base case if all positions filled return 1.
  • Create a 2d array of dp[N][6] initially filled with -1.
  • If the answer for a particular state is computed then save it in dp[i][j].
  • If the answer for a particular state is already computed then just return dp[i][j].

Below is the implementation of the above approach:

C++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
Ā 
const int MOD = 1e9 + 7;
Ā 
// DP table initialized with -1
int dp[1000001][6];
Ā 
// Recursive Function to count ways to
// create string of size N with no vowel
// is between two consonants and string
// does not start with A and does not
// end with Z.
int recur(int i, int j, int N, int id[])
{
Ā 
Ā Ā Ā Ā // Base case
Ā Ā Ā Ā if (i == N) {
Ā Ā Ā Ā Ā Ā Ā Ā return 1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // If answer for current state is
Ā Ā Ā Ā // already calculated then just
Ā Ā Ā Ā // return dp[i][j]
Ā Ā Ā Ā if (dp[i][j] != -1)
Ā Ā Ā Ā Ā Ā Ā Ā return dp[i][j];
Ā 
Ā Ā Ā Ā int ans = 0;
Ā 
Ā Ā Ā Ā // Traversing for all characters
Ā Ā Ā Ā for (int k = 1; k <= 26; k++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // First position cannot be A
Ā Ā Ā Ā Ā Ā Ā Ā if (i == 0 and k == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Last position cannot be Z
Ā Ā Ā Ā Ā Ā Ā Ā if (i == N - 1 and k == 26)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Let 1 represents vowel 0
Ā Ā Ā Ā Ā Ā Ā Ā // represents consonant trouble
Ā Ā Ā Ā Ā Ā Ā Ā // is 01 avoid 0 after that its
Ā Ā Ā Ā Ā Ā Ā Ā // fine to have 00, 10, and 11
Ā Ā Ā Ā Ā Ā Ā Ā // in our bitmask j
Ā Ā Ā Ā Ā Ā Ā Ā if (i <= 1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for vowel
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (id[k])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for consonant
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for taking vowel
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If last two are 01 that is
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // consonant after vowel then
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // avoid taking 0 that is
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // consonant for current
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // position for current
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // recursive call
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (j != 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Save and return dp value
Ā Ā Ā Ā return dp[i][j] = ans;
}
Ā 
// Function to count ways to create
// string of size N with no vowel is
// between two consonants and string
// does not start with A and does not
// end with Z.
int countWaysTo(int N)
{
Ā 
Ā Ā Ā Ā // Initializing dp array with - 1
Ā Ā Ā Ā memset(dp, -1, sizeof(dp));
Ā 
Ā Ā Ā Ā // Id to identify given characters
Ā Ā Ā Ā // whether they are vowel or consonant
Ā Ā Ā Ā int id[27];
Ā 
Ā Ā Ā Ā // Indicator id that indicates whether
Ā Ā Ā Ā // given character is vowel or consonant
Ā Ā Ā Ā for (int i = 1; i <= 26; i++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If it is equal to a, e, i, o or
Ā Ā Ā Ā Ā Ā Ā Ā // u then it is a vowel else
Ā Ā Ā Ā Ā Ā Ā Ā // consonant
Ā Ā Ā Ā Ā Ā Ā Ā if (i == 1 | i == 5 | i == 9 | i == 15 | i == 21)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 1;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 2;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return recur(0, 0, N, id);
}
Ā 
// Driver Code
int main()
{
Ā Ā Ā Ā // Input 1
Ā Ā Ā Ā int N = 1;
Ā 
Ā Ā Ā Ā // Function Call
Ā Ā Ā Ā cout << countWaysTo(N) << endl;
Ā 
Ā Ā Ā Ā // Input 2
Ā Ā Ā Ā int N1 = 2;
Ā 
Ā Ā Ā Ā // Function Call
Ā Ā Ā Ā cout << countWaysTo(N1) << endl;
Ā Ā Ā Ā return 0;
}


Java




// Java code to implement the approach
import java.io.*;
import java.util.*;
Ā 
class GFG {
Ā 
Ā Ā static final int MOD = (int)1e9 + 7;
Ā Ā static int[][] dp = new int[1000001][6];
Ā 
Ā Ā // Recursive Function to count ways to
Ā Ā // create string of size N with no vowel
Ā Ā // is between two consonants and string
Ā Ā // does not start with A and does not
Ā Ā // end with Z.
Ā Ā static int recur(int i, int j, int N, int[] id)
Ā Ā {
Ā Ā Ā Ā // Base case
Ā Ā Ā Ā if (i == N) {
Ā Ā Ā Ā Ā Ā return 1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // If answer for current state is
Ā Ā Ā Ā // already calculated then just
Ā Ā Ā Ā // return dp[i][j]
Ā Ā Ā Ā if (dp[i][j] != -1)
Ā Ā Ā Ā Ā Ā return dp[i][j];
Ā 
Ā Ā Ā Ā int ans = 0;
Ā 
Ā Ā Ā Ā // Traversing for all characters
Ā Ā Ā Ā for (int k = 1; k <= 26; k++) {
Ā 
Ā Ā Ā Ā Ā Ā // First position cannot be A
Ā Ā Ā Ā Ā Ā if (i == 0 && k == 1)
Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā // Last position cannot be Z
Ā Ā Ā Ā Ā Ā if (i == N - 1 && k == 26)
Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā // Let 1 represents vowel 0
Ā Ā Ā Ā Ā Ā // represents consonant trouble
Ā Ā Ā Ā Ā Ā // is 01 avoid 0 after that its
Ā Ā Ā Ā Ā Ā // fine to have 00, 10, and 11
Ā Ā Ā Ā Ā Ā // in our bitmask j
Ā Ā Ā Ā Ā Ā if (i <= 1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for vowel
Ā Ā Ā Ā Ā Ā Ā Ā if (id[k] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for consonant
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id);
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for taking vowel
Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If last two are 01 that is
Ā Ā Ā Ā Ā Ā Ā Ā // consonant after vowel then
Ā Ā Ā Ā Ā Ā Ā Ā // avoid taking 0 that is
Ā Ā Ā Ā Ā Ā Ā Ā // consonant for current
Ā Ā Ā Ā Ā Ā Ā Ā // position for current
Ā Ā Ā Ā Ā Ā Ā Ā // recursive call
Ā Ā Ā Ā Ā Ā Ā Ā if (j != 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id);
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Save and return dp value
Ā Ā Ā Ā return dp[i][j] = ans;
Ā Ā }
Ā 
Ā Ā // Function to count ways to create
Ā Ā // string of size N with no vowel is
Ā Ā // between two consonants and string
Ā Ā // does not start with A and does not
Ā Ā // end with Z.
Ā Ā static int countWaysTo(int N)
Ā Ā {
Ā Ā Ā Ā // Initializing dp array with - 1
Ā Ā Ā Ā for (int[] row : dp) {
Ā Ā Ā Ā Ā Ā Arrays.fill(row, -1);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Id to identify given characters
Ā Ā Ā Ā // whether they are vowel or consonant
Ā Ā Ā Ā int[] id = new int[27];
Ā 
Ā Ā Ā Ā // Indicator id that indicates whether
Ā Ā Ā Ā // given character is vowel or consonant
Ā Ā Ā Ā for (int i = 1; i <= 26; i++) {
Ā 
Ā Ā Ā Ā Ā Ā // If it is equal to a, e, i, o or
Ā Ā Ā Ā Ā Ā // u then it is a vowel else
Ā Ā Ā Ā Ā Ā // consonant
Ā Ā Ā Ā Ā Ā if (i == 1 || i == 5 || i == 9 || i == 15
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || i == 21)
Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 1;
Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 2;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return recur(0, 0, N, id);
Ā Ā }
Ā 
Ā Ā public static void main(String[] args)
Ā Ā {
Ā Ā Ā Ā // Input 1
Ā Ā Ā Ā int N = 1;
Ā 
Ā Ā Ā Ā // Function Call
Ā Ā Ā Ā System.out.println(countWaysTo(N));
Ā 
Ā Ā Ā Ā // Input 2
Ā Ā Ā Ā int N1 = 2;
Ā 
Ā Ā Ā Ā // Function Call
Ā Ā Ā Ā System.out.println(countWaysTo(N1));
Ā Ā }
}
Ā 
// This code is contributed by lokeshmvs21.


Python3




MOD = int(1e9 + 7)
Ā 
# DP table initialized with -1
dp = [[-1 for j in range(6)] for i in range(1000001)]
Ā 
# Recursive Function to count ways to
# create string of size N with no vowel
# is between two consonants and string
# does not start with A and does not
# end with Z.
def recur(i, j, N, id):
Ā Ā Ā Ā # Base case
Ā Ā Ā Ā if i == N:
Ā Ā Ā Ā Ā Ā Ā Ā return 1
Ā 
Ā Ā Ā Ā # If answer for current state is
Ā Ā Ā Ā # already calculated then just
Ā Ā Ā Ā # return dp[i][j]
Ā Ā Ā Ā if dp[i][j] != -1:
Ā Ā Ā Ā Ā Ā Ā Ā return dp[i][j]
Ā 
Ā Ā Ā Ā ans = 0
Ā 
Ā Ā Ā Ā # Traversing for all characters
Ā Ā Ā Ā for k in range(1, 27):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # First position cannot be A
Ā Ā Ā Ā Ā Ā Ā Ā if i == 0 and k == 1:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Last position cannot be Z
Ā Ā Ā Ā Ā Ā Ā Ā if i == N - 1 and k == 26:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Let 1 represents vowel 0
Ā Ā Ā Ā Ā Ā Ā Ā # represents consonant trouble
Ā Ā Ā Ā Ā Ā Ā Ā # is 01 avoid 0 after that its
Ā Ā Ā Ā Ā Ā Ā Ā # fine to have 00, 10, and 11
Ā Ā Ā Ā Ā Ā Ā Ā # in our bitmask j
Ā Ā Ā Ā Ā Ā Ā Ā if i <= 1:
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Recursive call for vowel
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if id[k] == 1:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Recursive call for consonant
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id)
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Recursive call for taking vowel
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If last two are 01 that is
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # consonant after vowel then
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # avoid taking 0 that is
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # consonant for current
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # position for current
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # recursive call
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if j != 1:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id)
Ā 
Ā Ā Ā Ā # Save and return dp value
Ā Ā Ā Ā dp[i][j] = ans
Ā Ā Ā Ā return ans
Ā 
# Function to count ways to create
# string of size N with no vowel is
# between two consonants and string
# does not start with A and does not
# end with Z.
def countWaysTo(N):
Ā Ā Ā Ā # Initializing dp array with - 1
Ā 
Ā Ā Ā Ā # Id to identify given characters
Ā Ā Ā Ā # whether they are vowel or consonant
Ā Ā Ā Ā id = [0 for i in range(27)]
Ā 
Ā Ā Ā Ā # Indicator id that indicates whether
Ā Ā Ā Ā # given character is vowel or consonant
Ā Ā Ā Ā for i in range(1, 27):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # If it is equal to a, e, i, o or
Ā Ā Ā Ā Ā Ā Ā Ā # u then it is a vowel else
Ā Ā Ā Ā Ā Ā Ā Ā # consonant
Ā Ā Ā Ā Ā Ā Ā Ā if i in [1, 5, 9, 15, 21]:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 1
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 2
Ā 
Ā Ā Ā Ā return recur(0, 0, N, id)
Ā 
# Driver Code
if __name__ == '__main__':
Ā Ā Ā Ā # Input 1
Ā Ā Ā Ā N = 1
Ā 
Ā Ā Ā Ā # Function Call
Ā Ā Ā Ā print(countWaysTo(N))
Ā 
Ā Ā Ā Ā # Input 2
Ā Ā Ā Ā N1 = 2
Ā 
Ā Ā Ā # Function Call
Ā Ā Ā Ā print(countWaysTo(N1))


C#




class Program
{
Ā Ā Ā Ā static int MOD = (int)1e9 + 7;
Ā Ā Ā Ā static int[][] dp = new int[1000001][];
Ā 
Ā Ā Ā Ā // Recursive Function to count ways to
Ā Ā Ā Ā // create string of size N with no vowel
Ā Ā Ā Ā // is between two consonants and string
Ā Ā Ā Ā // does not start with A and does not
Ā Ā Ā Ā // end with Z.
Ā Ā Ā Ā static int recur(int i, int j, int N, int[] id)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Base case
Ā Ā Ā Ā Ā Ā Ā Ā if (i == N)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If answer for current state is
Ā Ā Ā Ā Ā Ā Ā Ā // already calculated then just
Ā Ā Ā Ā Ā Ā Ā Ā // return dp[i][j]
Ā Ā Ā Ā Ā Ā Ā Ā if (dp[i][j] != -1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[i][j];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int ans = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traversing for all characters
Ā Ā Ā Ā Ā Ā Ā Ā for (int k = 1; k <= 26; k++)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // First position cannot be A
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == 0 && k == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Last position cannot be Z
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == N - 1 && k == 26)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Let 1 represents vowel 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // represents consonant trouble
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // is 01 avoid 0 after that its
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // fine to have 00, 10, and 11
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // in our bitmask j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i <= 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for vowel
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (id[k] == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for consonant
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for taking vowel
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If last two are 01 that is
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // consonant after vowel then
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // avoid taking 0 that is
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // consonant for current
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // position for current
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // recursive call
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (j != 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Save and return dp value
Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = ans;
Ā Ā Ā Ā Ā Ā Ā Ā return ans;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to count ways to create
Ā Ā Ā Ā // string of size N with no vowel is
Ā Ā Ā Ā // between two consonants and string
Ā Ā Ā Ā // does not start with A and does not
Ā Ā Ā Ā // end with Z.
Ā Ā Ā Ā static int countWaysTo(int N)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Initializing dp array with - 1
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < dp.Length; i++)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i] = new int[6];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < 6; j++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Id to identify given characters
Ā Ā Ā Ā Ā Ā Ā Ā // whether they are vowel or consonant
Ā Ā Ā Ā Ā Ā Ā Ā int[] id = new int
Ā Ā Ā Ā Ā Ā Ā Ā [27];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Indicator id that indicates whether
Ā Ā Ā Ā Ā Ā Ā Ā // given character is vowel or consonant
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= 26; i++)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If it is equal to a, e, i, o or
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // u then it is a vowel else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // consonant
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == 1 || i == 5 || i == 9 || i == 15 || i == 21)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 2;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā return recur(0, 0, N, id);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static void Main(string[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Input 1
Ā Ā Ā Ā Ā Ā Ā Ā int N = 1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function Call
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(countWaysTo(N));
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Input 2
Ā Ā Ā Ā Ā Ā Ā Ā int N1 = 2;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function Call
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(countWaysTo(N1));
Ā Ā Ā Ā }
}


Javascript




// Javascript code to implement the approach
const MOD = 1e9 + 7;
Ā 
// DP table initialized with -1
let dp = new Array(1000001);
for(let i = 0; i < 1000001; i++)
Ā Ā Ā Ā dp[i] = new Array(6).fill(-1);
Ā 
// Recursive Function to count ways to
// create string of size N with no vowel
// is between two consonants and string
// does not start with A and does not
// end with Z.
function recur(i, j, N, id)
{
Ā 
Ā Ā Ā Ā // Base case
Ā Ā Ā Ā if (i == N) {
Ā Ā Ā Ā Ā Ā Ā Ā return 1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // If answer for current state is
Ā Ā Ā Ā // already calculated then just
Ā Ā Ā Ā // return dp[i][j]
Ā Ā Ā Ā if (dp[i][j] != -1)
Ā Ā Ā Ā Ā Ā Ā Ā return dp[i][j];
Ā 
Ā Ā Ā Ā let ans = 0;
Ā 
Ā Ā Ā Ā // Traversing for all characters
Ā Ā Ā Ā for (let k = 1; k <= 26; k++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // First position cannot be A
Ā Ā Ā Ā Ā Ā Ā Ā if (i == 0 && k == 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Last position cannot be Z
Ā Ā Ā Ā Ā Ā Ā Ā if (i == N - 1 && k == 26)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Let 1 represents vowel 0
Ā Ā Ā Ā Ā Ā Ā Ā // represents consonant trouble
Ā Ā Ā Ā Ā Ā Ā Ā // is 01 avoid 0 after that its
Ā Ā Ā Ā Ā Ā Ā Ā // fine to have 00, 10, and 11
Ā Ā Ā Ā Ā Ā Ā Ā // in our bitmask j
Ā Ā Ā Ā Ā Ā Ā Ā if (i <= 1) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for vowel
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (id[k])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for consonant
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Recursive call for taking vowel
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, (2 * j) | 1, N, id);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If last two are 01 that is
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // consonant after vowel then
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // avoid taking 0 that is
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // consonant for current
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // position for current
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // recursive call
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (j != 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans += recur(i + 1, 2 * j, N, id);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Save and return dp value
Ā Ā Ā Ā return dp[i][j] = ans;
}
Ā 
// Function to count ways to create
// string of size N with no vowel is
// between two consonants and string
// does not start with A and does not
// end with Z.
function countWaysTo(N)
{
Ā Ā Ā Ā // Initializing dp array with - 1
Ā Ā Ā Ā for(let i=0; i<1000001; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā for(let j=0; j<6; j++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j]=-1;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Id to identify given characters
Ā Ā Ā Ā // whether they are vowel or consonant
Ā Ā Ā Ā let id = new Array(27);
Ā 
Ā Ā Ā Ā // Indicator id that indicates whether
Ā Ā Ā Ā // given character is vowel or consonant
Ā Ā Ā Ā for (let i = 1; i <= 26; i++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If it is equal to a, e, i, o or
Ā Ā Ā Ā Ā Ā Ā Ā // u then it is a vowel else
Ā Ā Ā Ā Ā Ā Ā Ā // consonant
Ā Ā Ā Ā Ā Ā Ā Ā if (i == 1 | i == 5 | i == 9 | i == 15 | i == 21)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 1;
Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā id[i] = 2;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return recur(0, 0, N, id);
}
Ā 
// Driver Code
// Input 1
let N = 1;
Ā 
// Function Call
console.log(countWaysTo(N));
Ā 
// Input 2
let N1 = 2;
Ā 
// Function Call
console.log(countWaysTo(N1));
Ā 
// This code is contributed by poojaagarwal2.


Output

24
625

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

Related Articles:

Feeling lost in the world of random DSA topics, wasting time without progress? Itā€™s time for a change! Join our DSA course, where weā€™ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfGā€™s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
21 Feb, 2023
Like Article
Save Article


Previous

<!ā€“

8 Min Readā€‚|ā€‚Java

ā€“>


Next


<!ā€“

8 Min Readā€‚|ā€‚Java

ā€“>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?