2022-1-17 T1220. Count the number of vowel sequences
Title Description:
Give you an integer ; n, please help us count how many strings with length ; can be formed according to the following rules:
Each character in the string should be a lowercase vowel ('a ',' e ',' I ',' o ',' U ')
Each vowel 'a' can only be followed by 'e'
Each vowel 'e' can only be followed by 'a' or 'I'
Each vowel 'I' cannot be followed by another 'I'
Each vowel 'o' can only be followed by 'I' or 'U'
Each vowel 'U' can only be followed by 'a'
Since the answer may be very large, please return the result after module 10 ^ 9 + 7.
Example:
Input: n = 2
Output: 10
Explanation: all possible strings are: "AE", "EA", "EI", "ia", "ie", "IO", "IU", "oi", "Ou" and "UA".
Idea:
Calculate the number of cases ending with this letter each time, and update the number of cases each time. The final result is the sum of five letters.
The time of each surplus is faster than that of the last surplus.
code:
class Solution {
public:
int countVowelPermutation(int n) {
const int maxValue = 1e9 + 7;
long a = 1, e = 1, i = 1, o = 1, u = 1, sumA, sumE, sumI, sumO, sumU;
for(int m = 2; m <= n; m++){
sumA = (e + u + i) % maxValue;
sumE = (a + i) % maxValue;
sumI = (e + o) % maxValue;
sumO = i;
sumU = (i + o) % maxValue;
a = sumA; e = sumE; i = sumI; o = sumO; u =sumU;
}
return (a + e + i + o + u) % maxValue;
}
};