Count Vowels Permutation (LeetCode 1220): Solution and Explanation
Count the strings of length n built from the vowels a, e, i, o, u where each vowel may only be followed by certain others. Return the count modulo 10^9 + 7.
Intuition
The rules only ever talk about two neighboring letters. Whether you can append a letter depends on nothing but the current last letter.
That means the whole history of a string can be forgotten except its final vowel. Five numbers are enough: how many valid strings of the current length end in a, e, i, o and u.
To extend by one letter, read the rules backwards. A string ending in 'a' after the step must have had 'e', 'i' or 'u' as its previous last letter, so the new count for 'a' is the sum of those three old counts.
Do this n - 1 times starting from all ones and add the five counts.
Approach: Five-state rolling DP
- Set
a = e = i = o = u = 1for strings of length 1. - Repeat
n - 1times:na = e + i + u,ne = a + i,ni = e + o,no = i,nu = i + o, each modulo 10^9 + 7. - Replace the old five counts with the new ones at once, so no update reads a value that was already changed this round.
- Return
(a + e + i + o + u) % (10^9 + 7).
Step-by-step example
n = 3
- Length 1: a = e = i = o = u = 1, total 5.
- Length 2: na = e + i + u = 3, ne = a + i = 2, ni = e + o = 2, no = i = 1, nu = i + o = 2. Total 10, matching ae, ea, ei, ia, ie, io, iu, oi, ou, ua.
- Length 3: na = 2 + 2 + 2 = 6, ne = 3 + 2 = 5, ni = 2 + 1 = 3, no = 2, nu = 2 + 1 = 3.
- Total 6 + 5 + 3 + 2 + 3 = 19.
Code: Python, Java, C++
1class Solution:
2 def countVowelPermutation(self, n: int) -> int:
3 MOD = 10**9 + 7
4 a = e = i = o = u = 1
5 for _ in range(n - 1):
6 na = (e + i + u) % MOD
7 ne = (a + i) % MOD
8 ni = (e + o) % MOD
9 no = i
10 nu = (i + o) % MOD
11 a, e, i, o, u = na, ne, ni, no, nu
12 return (a + e + i + o + u) % MOD1class Solution {
2 public int countVowelPermutation(int n) {
3 final long MOD = 1_000_000_007L;
4 long a = 1, e = 1, i = 1, o = 1, u = 1;
5 for (int step = 1; step < n; step++) {
6 long na = (e + i + u) % MOD;
7 long ne = (a + i) % MOD;
8 long ni = (e + o) % MOD;
9 long no = i;
10 long nu = (i + o) % MOD;
11 a = na; e = ne; i = ni; o = no; u = nu;
12 }
13 return (int) ((a + e + i + o + u) % MOD);
14 }
15}1class Solution {
2public:
3 int countVowelPermutation(int n) {
4 const long long MOD = 1000000007LL;
5 long long a = 1, e = 1, i = 1, o = 1, u = 1;
6 for (int step = 1; step < n; step++) {
7 long long na = (e + i + u) % MOD;
8 long long ne = (a + i) % MOD;
9 long long ni = (e + o) % MOD;
10 long long no = i;
11 long long nu = (i + o) % MOD;
12 a = na; e = ne; i = ni; o = no; u = nu;
13 }
14 return (int) ((a + e + i + o + u) % MOD);
15 }
16};Time and space complexity
O(n)
Each of the n - 1 steps does a constant amount of work for five vowels.
O(1)
Only the five current counts and five new counts are stored.
Other approaches
Full DP table (time O(n), space O(n))
Store dp[len][v] for every length. It is easier to debug but uses O(n) memory for no gain, since each row depends only on the one before it.
Matrix exponentiation (time O(log n), space O(1))
The update is a fixed 5x5 linear map, so raising its matrix to the power n - 1 with fast exponentiation gives the counts in O(125 log n). It matters only when n is huge.
Common mistakes
- Updating a, e, i, o, u in place one after another. Later formulas then read values from the new length instead of the old one.
- Writing the transitions forwards instead of backwards. The rules say who may follow whom; the update needs who may precede each vowel.
- Forgetting the modulo on the final sum. Five values below 10^9 + 7 can add up past it.
- Overflowing 32-bit integers in Java, C++ or C#. Sums of three counts can exceed 2^31, so use 64-bit values.
Frequently asked questions
Why is 'no = i' with no other terms?
Only 'i' may be followed by 'o'. 'a' must be followed by 'e', 'e' by 'a' or 'i', 'o' by 'i' or 'u', and 'u' by 'a', so 'i' is the only possible predecessor of 'o'.
Is this the same as counting walks in a graph?
Yes. Draw an arrow from each vowel to the vowels that may follow it. A valid string of length n is a walk of n - 1 arrows, and the DP counts walks by where they end.
How does this relate to Knight Dialer?
Knight Dialer has exactly the same shape: ten digit states and a fixed set of allowed moves, counted step by step with a rolling array.