【leetcode】1220. Count Vowels Permutation
题目如下:
Given an integer
n, your task is to count how many strings of lengthncan 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: 68Constraints:
1 <= n <= 2 * 10^4
解题思路:用动态规划的方法,记dp[i][j] 为第i个位置放第j个元音字母的情况下可以组成的字符串的个数,很容易得到状态转移方程。例如如果第i个元素是'e',那么第i-1的元素就只能是'a'或者'i',有 dp[i]['e'] = dp[i-1]['a'] + dp[i-1]['i'],最后只有求出第n个元素放这五个元音字母的个数的和即可。
代码如下:
class Solution(object):
def countVowelPermutation(self, n):
"""
:type n: int
:rtype: int
"""
dp = [[0] * 5 for _ in range(n)]
vowels = ['a', 'e', 'i', 'o', 'u']
dp[0][0] = dp[0][1] =dp[0][2] =dp[0][3] = dp[0][4] = 1
for i in range(1,len(dp)):
for j in range(len(vowels)):
if vowels[j] == 'a':
dp[i][j] += dp[i - 1][vowels.index('e')]
dp[i][j] += dp[i - 1][vowels.index('u')]
dp[i][j] += dp[i - 1][vowels.index('i')]
elif vowels[j] == 'e':
dp[i][j] += dp[i - 1][vowels.index('a')]
dp[i][j] += dp[i - 1][vowels.index('i')]
elif vowels[j] == 'i':
dp[i][j] += dp[i - 1][vowels.index('e')]
dp[i][j] += dp[i - 1][vowels.index('o')]
elif vowels[j] == 'o':
dp[i][j] += dp[i - 1][vowels.index('i')]
elif vowels[j] == 'u':
dp[i][j] += dp[i - 1][vowels.index('i')]
dp[i][j] += dp[i - 1][vowels.index('o')]
return sum(dp[-1]) % (10**9 + 7)
【leetcode】1220. Count Vowels Permutation的更多相关文章
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【LeetCode】 204. Count Primes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...
- 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】38 - Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
随机推荐
- Oracle常用操作表空间sql脚本
--删除用户(如果用户下面有对象,需要加cascade参数) drop user zxasrs cascade; --修改表空间为离线 alter tablespace zxasrs offline; ...
- Linux进程:管理和调度
一:进程管理 进程.轻量级进程和线程 通常定义:进程是程序执行时的一个实例. 这个很像类和实例对象的关系.从内核来看:进程的目的就是担当分配系统资源(CPU,内存等)的实体. 当进程创建时,它几乎和父 ...
- Adobe出品(支持IOS,android,web调用)免费插件编辑图片
<head runat="server"><meta http-equiv="Content-Type" content="text ...
- excel常用公式--逻辑运算类
if: IF(logical_test, value_if_true, [value_if_false]). and: 逻辑判断,相当于“并”. or: 逻辑判断,相当于“或”.
- CF486B OR in Matrix(构造+思维)
CF486B 一道有趣的思维题 由于or的性质可知只要a[i][j]为1那么b中第i行,第j列将都变成1 相反的,如果b[i][j]是0那么a中第i行,第j列都必须是0 根据第二个性质我们可以构造出a ...
- 面试--hr常问的问题
程序员换工作,会有技术面试(可能不止一轮的技术面),还会有hr的面试,技术面主要是偏向于技术问题,hr面试主要问的一些问题,下面做下汇总: 1.你换工作的原因,你为何辞职 必问的问题,送分题或者送命题 ...
- mysql修改max_allowed_packet数据包最大值
在windows环境下!!!! 1.找到my.inc文件,不是你的安装目录路径,是C:\ProgramData\MySQL\MySQL Server 5.7这个路径,注意 ProgramData 文件 ...
- svn下载项目的时候出现 Path to certificate
svn关联的时候出现这种情况,并且有svn的账号的时候,可以找setting中Version Control 中的Subversion中celar 一下即可,然后再重新下载就会让你重新输入用户名和密码 ...
- 链接Caffe,程序报错应用程序无法正常启动(0xc000007b)
目录 背景 Debug 解决办法 原因(猜想) 总结 重点是介绍了一种排查这个问题的方法. 背景 Windows 下, Caffe 单独编译成库并且安装在路径 Caffe_DIR, 动态链接库 Caf ...
- luoguP3261_[JLOI2015]城池攻占
题意 有一棵树\(n\)个节点,每个节点有一个防御值,以及两个属性,表示一个骑士占领该节点后攻击值是加还是乘,有\(m\)个骑士,有初始位置和初始攻击值,如果攻击值大于该节点的防御值,就能占领该节点, ...