标签:Count may Permutation Vowels long vowel Each icount mod
Given an integer n
, your task is to count how many strings of length n
can be formed under the following rules:
- Each character is a lower case vowel (
'a'
,'e'
,'i'
,'o'
,'u'
) - Each vowel
'a'
may only be followed by an'e'
. - Each vowel
'e'
may only be followed by an'a'
or an'i'
. - Each vowel
'i'
may not be followed by another'i'
. - Each vowel
'o'
may only be followed by an'i'
or a'u'
. - Each vowel
'u'
may only be followed by an'a'.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: n = 1 Output: 5 Explanation: All possible strings are: "a", "e", "i" , "o" and "u".
Example 2:
Input: n = 2 Output: 10 Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".
Example 3:
Input: n = 5 Output: 68
Constraints:
1 <= n <= 2 * 10^4
class Solution { public int countVowelPermutation(int n) { long acount = 1, ecount = 1, icount = 1, ocount = 1, ucount = 1; int mod = 1000000007; for(int i = 1; i < n; i++) { long newac = (ecount + icount + ucount) % mod; long newec = (acount + icount) % mod; long newic = (ecount + ocount) % mod; long newoc = (icount) % mod; long newuc = (icount + ocount) % mod; acount = newac; ecount = newec; icount = newic; ocount = newoc; ucount = newuc; } return (int) ((acount + ecount + icount + ocount + ucount) % mod); } }
hard??
dp, 对应的是以这个字母为结束的这么多位数的个数
标签:Count,may,Permutation,Vowels,long,vowel,Each,icount,mod 来源: https://www.cnblogs.com/wentiliangkaihua/p/14970757.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。