

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Vowels Permutation in C++
Suppose we have one number n, we have to count how many strings of length n can be formed using these rules − Each character is a lower case vowel Each vowel 'a' may only be followed by an 'e'. Each vowel 'e' may only be followed by an 'a' or 'i'. Each vowel 'i' may not be followed by another 'i'. Each vowel 'o' may only be followed by an 'i' or 'u'. Each vowel 'u' may only be followed by an 'a'. The answer may be too large, so we will find the answer modulo 10^9 + 7.
So, if the input is like 2, then the output will be 10, this is because all possible strings are "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou", "ua".
To solve this, we will follow these steps −
m = 1^9 + 7
Define a function add(), this will take a, b,
return ((a mod m) + (b mod m)) mod m
Define a function mul(), this will take a, b,
return ((a mod m) * (b mod m)) mod m
Define a function solve(), this will take n,
Define an array A of size: 5 x 5 := {{0,1,0,0,0},{1,0,1,0,0},{1,1,0,1,1},{0,0,1,0,1},{1,0,0,0,0}}
Define an array result of size: 5 x 5.
for initialize i := 0, when i < 5, update (increase i by 1), do −
for initialize j := 0, when j < 5, update (increase j by 1), do −
if i is same as j, then, result[i, j] := 1
Otherwise,result[i, j] := 0
(decrease n by 1)
for initialize i := 1, when i <= n, update (increase i by 1), do −
results = result * A
sum := 0
for initialize i := 0, when i < 5, update (increase i by 1), do −
for initialize j := 0, when j < 5, update (increase j by 1), do −
sum := add(result[i, j], sum)
return sum
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; typedef long long int lli; const lli m = 1e9+7; lli add(lli a, lli b){ return ((a%m) + (b%m))%m; } lli mul(lli a, lli b){ return ((a%m) * (b%m))%m; } class Solution { public: void multiply(lli A[5][5], lli B[5][5]){ lli C[5][5]; for(lli i =0;i<5;i++){ for(lli j=0;j<5;j++){ lli temp =0; for(lli k =0;k<5;k++){ temp = add(temp,mul(A[i][k],B[k][j])); } C[i][j] = temp; } } for(lli i =0;i<5;i++){ for(lli j =0;j<5;j++){ A[i][j] = C[i][j]; } } } lli solve(lli n){ lli A[5][5] = { { 0, 1, 0, 0, 0 }, { 1, 0, 1, 0, 0 }, { 1, 1, 0, 1, 1 }, { 0, 0, 1, 0, 1 }, { 1, 0, 0, 0, 0 } }; lli result[5][5]; for (lli i = 0; i < 5; i++) { for (lli j = 0; j < 5; j++) { if (i == j) result[i][j] = 1; else result[i][j] = 0; } } n--; for (int i = 1; i <= n; i++) multiply(result, A); lli sum = 0; for (lli i = 0; i < 5; i++) { for (lli j = 0; j < 5; j++) { sum = add(result[i][j], sum); } } return sum; } int countVowelPermutation(int n) { return solve(n); } }; main(){ Solution ob; cout << (ob.countVowelPermutation(2)); }
Input
2
Output
10
- Related Questions & Answers
- C# Program to count vowels in a string
- Java Program to count vowels in a string
- Count and display vowels in a string in Python
- Java Program to count all vowels in a string
- C++ Program to count Vowels in a string using Pointer?
- To count Vowels in a string using Pointer in C++ Program
- Count the pairs of vowels in the given string in C++
- Java program to count the number of vowels in a given sentence
- C# Program to count number of Vowels and Consonants in a string
- How to Count the Number of Vowels in a string using Python?
- Python program to count number of vowels using set in a given string
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Count natural numbers whose all permutation are greater than that number in C++
- C Program to count vowels, digits, spaces, consonants using the string concepts
- How to count number of vowels and consonants in a string in C Language?