★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:为敢(WeiGanTechnologies)
➤我的域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xxnlrxlu-dq.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an integer n
, your task is to count how many strings of length n
can be formed under the following rules:git
'a'
, 'e'
, 'i'
, 'o'
, 'u'
)'a'
may only be followed by an 'e'
.'e'
may only be followed by an 'a'
or an 'i'
.'i'
may not be followed by another 'i'
.'o'
may only be followed by an 'i'
or a 'u'
.'u'
may only be followed by an 'a'.
Since the answer may be too large, return it modulo 10^9 + 7.
github
Example 1:微信
Input: n = 1 Output: 5 Explanation: All possible strings are: "a", "e", "i" , "o" and "u".
Example 2:spa
Input: n = 2 Output: 10 Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".
Example 3: code
Input: n = 5 Output: 68
Constraints:orm
1 <= n <= 2 * 10^4
给你一个整数 n
,请你帮忙统计一下咱们能够按下述规则造成多少个长度为 n
的字符串:htm
'a'
, 'e'
, 'i'
, 'o'
, 'u'
)'a'
后面都只能跟着 'e'
'e'
后面只能跟着 'a'
或者是 'i'
'i'
后面 不能 再跟着另外一个 'i'
'o'
后面只能跟着 'i'
或者是 'u'
'u'
后面只能跟着 'a'
因为答案可能会很大,因此请你返回 模 10^9 + 7
以后的结果。blog
示例 1:字符串
输入:n = 1 输出:5 解释:全部可能的字符串分别是:"a", "e", "i" , "o" 和 "u"。
示例 2:
输入:n = 2 输出:10 解释:全部可能的字符串分别是:"ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" 和 "ua"。
示例 3:
输入:n = 5 输出:68
提示:
1 <= n <= 2 * 10^4
1 class Solution { 2 func countVowelPermutation(_ n: Int) -> Int { 3 var dp:[[Int]] = [[Int]](repeating: [Int](repeating: 0, count: 5), count: n + 1) 4 let MOD:Int = 1000000007 5 for i in 0..<5 6 { 7 dp[1][i] = 1 8 } 9 for i in 1..<n 10 { 11 dp[i+1][0] = (dp[i][1] + dp[i][2] + dp[i][4]) % MOD 12 dp[i+1][1] = (dp[i][0] + dp[i][2]) % MOD 13 dp[i+1][2] = (dp[i][1] + dp[i][3]) % MOD 14 dp[i+1][3] = dp[i][2] 15 dp[i+1][4] = (dp[i][2] + dp[i][3]) % MOD 16 } 17 var res:Int = 0 18 for i in 0..<5 19 { 20 res = (res + dp[n][i]) % MOD 21 } 22 return res 23 } 24 }