【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. ...
随机推荐
- goland搭建beego开发环境
1.安装最新的go软件 ,当前版本1.122.下载goland开发工具3.安装bee工具 go get github.com/beego/bee4.通过bee api dsh -tables=&quo ...
- 基于Opencv的自适应中值滤波函数selfAdaptiveMedianBlur()
7.3.3 自适应滤波器 自适应中值滤波器 对于7.3.2节所讨论的中值滤波器,只要脉冲噪声的空间密度不大,性能还是可以的(根据经验需Pa和Pb小于0.2).本节将证明,自适应中值滤波器可以处理更大概 ...
- C++ 简介 基本语法 注释
一.第一个 C++ 程序 #include <iostream> using namespace std; int main() { cout << "Hello, ...
- scrapy 启动
虚拟环境安装好了之后,scrapy 框架安装好了以后: workon article_spider (项目名称) scrapy startproject Article Spider 工程目录 ...
- 优秀编程学习网站&博文记录
记录优秀讲解知识点博客内容,侵删! 编程者学习网站 LearnKu终身编程者的知识社区 自动化测试内容 Python 接口自动化测试 应用开源接口网站:https://httpbin.org/#/St ...
- java操作ElasticSearch(es)进行增删查改操作
有时间是要了解一下ES这个东西的~ ---------------------------------------------------------------------------------- ...
- Python 入门之 内置模块 -- 序列化模块(json模块、pickle模块)
Python 入门之 内置模块 -- 序列化模块(json模块.pickle模块) 1.序列化 Python中这种序列化模块有三种: json模块 : 不同语言都遵循的一种数据转化格式,即不同 ...
- 02、CDF文件
有了探针排布图像的基础,我们就可以更好地理解CDF文件了.假如每个探针的位置用一个坐标表示,以左上角为(0,0),那么整张芯片的坐标就如下图(行数n必须等于列数m,这里共有n*m个探针): 0,0 1 ...
- webpack4.x + vue2.x 构建前端工程化(1)
本篇文篇纯属个人笔记,实现工程化打包(用打包后的文件可以正常渲染页面),后续继续更新配置开发环境与生产环境,如果有不合理的地方还望各位指点! 不用脚手架,直接用vue和webpack搭建前端工程化项目 ...
- 常用的框架伪静态(Apache转Nginx)
EmpireCMS: rewrite ^([^\.]*)/listinfo-(.+?)-(.+?)\.html$ $/e/action/ListInfo/index.php?classid=$& ...