题目如下:

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. React-Native中props用法详解

    props就是属性,是为了描述一个组件的特征而存在的.它是父组件传递给子组件的. 使用props 通过上一个页面传递 新建一个 PropsTest.js 文件 1 2 3 4 5 exprot def ...

  2. CentOS 7 卸载 mysql

    查看是否安装 mysql rpm -qa | grep -i mysql yum list install mysql* 卸载 yum方式 yum remove mysql mysql-server ...

  3. MySQL改密

    一.未进入之前: 1.grep password /var/log/mysqld.log   得出默认密码2.更改密码    mysqladmin -uroot -p'd-tlbwIgP3e2' pa ...

  4. 炼丹的一些trick

    采摘一些大佬的果实: 知乎:如何理解深度学习分布式训练中的large batch size与learning rate的关系? https://blog.csdn.net/shanglianlm/ar ...

  5. magento form.html不显示 window 和 Linux下的区别

    window 无大小写区别,所以可以显示表框 Linux 大小写敏感,显示不了 \app\code\community\Company\BabyPay\Model\Payment.php 里定义了fo ...

  6. [转贴]linux lsof命令详解

    linux lsof命令详解 https://www.cnblogs.com/sparkbj/p/7161669.html 简介 lsof(list open files)是一个列出当前系统打开文件的 ...

  7. Notepad++ 不打开历史文件

    1. 自己的很多虚拟机上面安装了notepad++ 提高编辑文件的速度. 但是发现 有时候总是默认打开 很多 历史文件 会造成很卡顿. 2. 解决办法 如下图 设置->首选项 3. 具体的位置为 ...

  8. [codeforces940E]Cashback

    题目链接 题意是说将$n$个数字分段使得每段贡献之和最小,每段的贡献为区间和减去前$\left \lfloor \frac{k}{c}\right \rfloor$小的和. 仔细分析一下可以知道,减去 ...

  9. POJ - 2421 Constructing Roads(最小生成树&并查集

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  10. P1550打井

    这是USACO2008年的一道最小生成树题,感谢dzj老师那天教的图论. 要引渠让每一个村庄都可以接到水,然后从某一个村庄到另一个村庄修剪水道要花费w元,并且还要打井(至少一个)(而输入数据也包括了在 ...