【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. ...
随机推荐
- 【miscellaneous】网络摄像机
自20世纪90年代初期网络摄像机开始诞生,产业已历经20余年的演变. "IP大时代"的口号在安防领域已响彻已久,但也是自2015年至今才开使有了真正的底气.当全面超越模拟已尘埃落定 ...
- wget断点续传下载需要登录的网站上的大文件
1 举个例子 xcode 2 方法 wget --load-cookies=cookies.txt -c url -c是断点续传,如果网络断了,再运行该命令会接着最新的下载继续下载. --load- ...
- mysql应用之通过存储过程方式批量插入数据
我们平时的测试过程中有一个环节就是准备测试数据,包括准备基础数据,准备业务数据,使用的场景包括压力测试,后台批量数据传输,前端大数据查询导出,或者分页打印等功能,准备测试数据我们通俗点讲就是造数据,根 ...
- 洛谷 P1073 最优贸易 题解
题面 大家都是两遍SPFA吗?我这里就一遍dp啊: 首先判断对于一个点u,是否可以从一号点走到这里,并且可以从u走到n号点: 对于这样的点我们打上标记: 那么抛出水晶球的点一定是从打上标记的点中选出一 ...
- 洛谷 P3919 可持久化线段树 题解
题面 这题好水的说~很明显就是主席树的大板子 然而我交了3遍才调完所有的BUG,开好足够的数组,卡掉大大的常数: 针对与每次操作,change()会创建新节点,而ask()虽然也会更新左右儿子的节点编 ...
- Luogu P2839 [国家集训队]middle
题目 首先我们考虑解决中位数一类问题的常用手段:二分\(mid\),将大于等于它的设为\(1\),小于它的设为\(−1\),判断区间和是否\(\ge0\). 对于询问\(a,b,c,d\),二分完\( ...
- 列表(索引切片 增删改查 嵌套) range 元组的初识
li = ["alex", "WuSir", "ritian", "barry", "wenzhou" ...
- 【BZOJ-4289】Tax 最短路 + 技巧建图(化边为点)
题意 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边权N<=10 ...
- css3之新增伪类
css3新增了许多伪类,但是IE8以及更低版本的IE浏览器不支持css3伪类,所以在使用时要是涉及到布局等意象全局的样式,应该多考虑一下. 1.elem:nth-child(n) 这个伪类选中父元素下 ...
- node端口被占用
Error: listen EADDRINUSE :::8000 at Object._errnoException (util.js:992:11) at _exceptionWithHostPor ...