相关信息:
LeetCode链接:
https://leetcode-cn.com/problems/count-vowels-permutation/
代码:
//作者:LeetCode-Solution
public class L1220 {
public static void main(String[] args) {
//Test
SolutionL1220 solutionL1220 = new SolutionL1220();
int ans = solutionL1220.countVowelPermutation(3);
System.out.println(ans);
}
}
class SolutionL1220 {
public int countVowelPermutation(int n) {
// modvalue is set to limit the size of the returned value in subsequent mod operations to [0, value]
long modValue = 1000000007;
long[] dp = new long[5];
long[] tempdp = new long[5];
/*initializate dp[i]
* i express 0:a 1:e 2:u 3:o 4:i
* the value of dp[i] set to 1 to indicate the value passed in n = 1 situation
* */
for (int i = 0; i < 5; ++i) {
//
dp[i] = 1;
}
for (int i = 2; i <= n; ++i) {
/* a前面可以为e,u,i */
tempdp[0] = (dp[1] + dp[2] + dp[4]) % modValue;
/* e前面可以为a,i */
tempdp[1] = (dp[0] + dp[2]) % modValue;
/* i前面可以为e,o */
tempdp[2] = (dp[1] + dp[3]) % modValue;
/* o前面可以为i */
tempdp[3] = dp[2];
/* u前面可以为i,o */
tempdp[4] = (dp[2] + dp[3]) % modValue;
/*Copy five consecutive values from the position where the index of tempdp array is 0,
and put them into the dp array with the starting index of 0 in turn*/
System.arraycopy(tempdp, 0, dp, 0, 5);
}
long ans = 0;
/*add up the value of dp array
* mod operation is to limit the numerical size of variable ans
* */
for (int i = 0; i < 5; ++i) {
ans = (ans + dp[i]) % modValue;
}
return (int)ans;
}
}