Count Vowels Permutation — Solution in Python, Java & C++
Count Vowels Permutation solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A hard problem.
Count Vowels Permutation is a hard coding problem on dynamic programming, asked in Amazon, Google, Microsoft and MathWorks interviews.
- Difficulty: Hard
- Topics: Dynamic Programming
- Asked at: Amazon, Google, Microsoft, MathWorks
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer n, count the strings of length n over the vowels a, e, i, o, u that obey every rule:
amay only be followed bye.emay only be followed byaori.imay not be followed by anotheri.omay only be followed byioru.umay only be followed bya.
Return the count modulo 10^9 + 7.
Example 1
Input: n = 1
Output: 5
Explanation: All five single vowels qualify.
Example 2
Input: n = 2
Output: 10
Example 3
Input: n = 5
Output: 68
Constraints
1 <= n <= 2000
Hints for Count Vowels Permutation
Hint 1
Let
dp[v]be the number of valid strings of the current length that end in vowelv.Hint 2
Extending by one character redistributes those counts along the allowed transitions.
Hint 3
The
irule is easiest read backwards:imay be preceded by anything exceptiitself.
How to solve Count Vowels Permutation
Keep the count of valid strings ending in each vowel; extending by one character redistributes counts along the allowed transitions read backwards (what may precede each vowel).
Approach
- Start every vowel at 1 (length 1).
- Repeat
n - 1times:a' = e + i + u,e' = a + i,i' = e + o,o' = i,u' = i + o, all modulo10⁹+7. - Return the sum.
Why it works
The rules define a directed graph on the five vowels; the number of length-n walks ending at v is the sum over its in-neighbours of length-n-1 walks, which is exactly the recurrence.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Derive the backward adjacency carefully:
acan be preceded bye,i,ubecause those are the vowels allowed to be followed bya.
Reference solution
Python
def countVowelPermutation(n: int) -> int:
MOD = 1000000007
a = e = i = o = u = 1
for _ in range(n - 1):
a, e, i, o, u = (e + i + u) % MOD, (a + i) % MOD, (e + o) % MOD, i % MOD, (i + o) % MOD
return (a + e + i + o + u) % MODJava
public static int countVowelPermutation(int n) {
final long MOD = 1000000007L;
long a = 1, e = 1, i = 1, o = 1, u = 1;
for (int step = 1; step < n; step++) {
long na = (e + i + u) % MOD;
long ne = (a + i) % MOD;
long ni = (e + o) % MOD;
long no = i % MOD;
long nu = (i + o) % MOD;
a = na; e = ne; i = ni; o = no; u = nu;
}
return (int) ((a + e + i + o + u) % MOD);
}C++
int countVowelPermutation(int n) {
const long long MOD = 1000000007LL;
long long a = 1, e = 1, i = 1, o = 1, u = 1;
for (int step = 1; step < n; step++) {
long long na = (e + i + u) % MOD;
long long ne = (a + i) % MOD;
long long ni = (e + o) % MOD;
long long no = i % MOD;
long long nu = (i + o) % MOD;
a = na; e = ne; i = ni; o = no; u = nu;
}
return (int) ((a + e + i + o + u) % MOD);
}JavaScript
var countVowelPermutation = function(n) {
const MOD = 1000000007;
let a = 1, e = 1, i = 1, o = 1, u = 1;
for (let step = 1; step < n; step++) {
const na = (e + i + u) % MOD;
const ne = (a + i) % MOD;
const ni = (e + o) % MOD;
const no = i % MOD;
const nu = (i + o) % MOD;
a = na;
e = ne;
i = ni;
o = no;
u = nu;
}
return (a + e + i + o + u) % MOD;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 402 dynamic programming problems · the whole catalogue
Learn the technique: Dynamic Programming