2021-01-25 1641. Count Sorted Vowel Strings

4 篇文章 0 订阅

1641. Count Sorted Vowel Strings

Difficulty: Medium

Related Topics: Math, Dynamic Programming, Backtracking

Given an integer n, return the number of strings of length n that consist only of vowels (a, e, i, o, u) and are lexicographically sorted.

A string s is lexicographically sorted if for all valid i, s[i] is the same as or comes before s[i+1] in the alphabet.

Example 1:

Input: n = 1
Output: 5
Explanation: The 5 sorted strings that consist of vowels only are ["a","e","i","o","u"].

Example 2:

Input: n = 2
Output: 15
Explanation: The 15 sorted strings that consist of vowels only are
["aa","ae","ai","ao","au","ee","ei","eo","eu","ii","io","iu","oo","ou","uu"].
Note that "ea" is not a valid string since 'e' comes after 'a' in the alphabet.

Example 3:

Input: n = 33
Output: 66045

Constraints:

  • 1 <= n <= 50
Solution

Language: Java

Keys: in dynamic programming, we need to construct a dp table, and in a dp table, more often than not, one point is related to the two points at its two corners.

class Solution {
    public int countVowelStrings(int n) {
        int[][] result = new int[n + 1][6];
        
        for (int i = 1; i < 6; i++) {
            result[1][i] = i;
        }
        
        for (int i = 2; i <= n; i++) {
            result[i][1] = 1;
            for (int vo = 1; vo < 6; vo++) {
                result[i][vo] = result[i - 1][vo] + result[i][vo - 1];
            }
        }
        
        return result[n][5];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值