zoukankan      html  css  js  c++  java
  • [Swift]LeetCode1220. 统计元音字母序列的数目 | Count Vowels Permutation

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(let_us_code)
    ➤博主域名:https://www.zengqiang.org
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/11626363.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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

    给你一个整数 n,请你帮忙统计一下我们可以按下述规则形成多少个长度为 n 的字符串:

    • 字符串中的每个字符都应当是小写元音字母('a''e''i''o''u'
    • 每个元音 'a' 后面都只能跟着 'e'
    • 每个元音 'e' 后面只能跟着 'a' 或者是 'i'
    • 每个元音 'i' 后面 不能 再跟着另一个 'i'
    • 每个元音 'o' 后面只能跟着 'i' 或者是 'u'
    • 每个元音 'u' 后面只能跟着 'a'

    由于答案可能会很大,所以请你返回 模 10^9 + 7 之后的结果。

    示例 1:

    输入:n = 1
    输出:5
    解释:所有可能的字符串分别是:"a", "e", "i" , "o" 和 "u"。
    

    示例 2:

    输入:n = 2
    输出:10
    解释:所有可能的字符串分别是:"ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" 和 "ua"。
    

    示例 3:

    输入:n = 5
    输出:68

    提示:

    • 1 <= n <= 2 * 10^4

    Runtime: 100 ms
    Memory Usage: 21.2 MB
     1 class Solution {
     2     func countVowelPermutation(_ n: Int) -> Int {
     3         var dp:[[Int]] = [[Int]](repeating: [Int](repeating: 0, count: 5), count: n + 1)
     4         let MOD:Int = 1000000007
     5         for i in 0..<5
     6         {
     7             dp[1][i] = 1
     8         }
     9         for i in 1..<n
    10         {
    11             dp[i+1][0] = (dp[i][1] + dp[i][2] + dp[i][4]) % MOD
    12             dp[i+1][1] = (dp[i][0] + dp[i][2]) % MOD
    13             dp[i+1][2] = (dp[i][1] + dp[i][3]) % MOD
    14             dp[i+1][3] = dp[i][2]
    15             dp[i+1][4] = (dp[i][2] + dp[i][3]) % MOD
    16         }
    17         var res:Int = 0
    18         for i in 0..<5
    19         {
    20             res = (res + dp[n][i]) % MOD
    21         }
    22         return res
    23     }
    24 }
  • 相关阅读:
    python django blog
    Django框架的学习
    Django 与 Flask框架的比较
    pycharm windows 远程修改服务器代码
    matlab的Deep Learning的toolbox 中的SAE算法
    什么是有监督学习和无监督学习
    linux 操作系统rz sz 快速上传和下载文件
    PDF,word ,PPT,等各种文件转换在线工具(免费)
    linux 安装 rz sz 快速上传和下载文件
    sci-hub 下载地址更新
  • 原文地址:https://www.cnblogs.com/strengthen/p/11626363.html
Copyright © 2011-2022 走看看