题目如下:

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

解题思路:用动态规划的方法,记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的更多相关文章

  1. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  2. 【LeetCode】 204. Count Primes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...

  3. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

  4. 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...

  5. 【LeetCode】696. Count Binary Substrings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...

  6. 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】38 - Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  8. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  9. 【Leetcode】357. Count Numbers with Unique Digits

    题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...

随机推荐

  1. 剑指Offer总结——用两个栈实现队列

    class Solution { public: void push(int node) { stack2.push(node); } int pop() { if(stack1.empty()){ ...

  2. java反射机制学习笔记

    内容引用自:https://www.cnblogs.com/wkrbky/p/6201098.html https://www.cnblogs.com/xumBlog/p/8882489.html,本 ...

  3. lambda得用法

  4. Akka系列(四):Akka中的共享内存模型

    前言...... 通过前几篇的学习,相信大家对Akka应该有所了解了,都说解决并发哪家强,JVM上面找Akka,那么Akka到底在解决并发问题上帮我们做了什么呢? 共享内存 众所周知,在处理并发问题上 ...

  5. hue数据导出

    1/ 执行要下载的sql语句 2/  执行完成后,点击下面导出 3.选择所有 4.选择每个人对应的文件夹,没有自己名称的可以创建一个 5.选择导出 6.右面是导出执行时的界面 7.选择已经执行的文件 ...

  6. IOMETER的简单使用

    1. 网上下载文件: 一般至少包含两个: 2. 使用IOmeter 进行 功能测试. 注意选择 测试需要的盘 注意 选择的磁盘 会被充满. 会产生一个特别大的文件 3. 选择测试对象 4. 可以查看实 ...

  7. Windows Forms和WPF在Net Core 3.0框架下并不会支持跨平台

    Windows Forms和WPF在Net Core 3.0框架下并不会支持跨平台 微软将WinForms和WPF带到.NET Core 3.0这一事实,相信大家都有所了解,这是否意味着它在Linux ...

  8. python中常见的一些错误异常类型

    python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 什么是异常? 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的 ...

  9. 吴恩达深度学习:python中的广播

    1.python中的广播: (1)广播是一种手段,可以让python代码执行得更快,我们来看看python实际如何执行. 下面矩阵列出了100克苹果.牛肉.鸡蛋和蛋白质中含有的碳水化合物.蛋白质和脂肪 ...

  10. H5头部meta标签的作用

    <!DOCTYPE html>  H5标准声明,使用 HTML5 doctype,不区分大小写 <head lang=”en”> 标准的 lang 属性写法 <meta ...