[leetcode] Count the number of lexicographic vowel strings (dp, 3 ways of writing)

Given an integer n, return the number of strings of length n consisting of vowels only (a, e, i, o, u) in lexicographic order.

The lexicographical arrangement of a string s requires that for all valid is, the position of s[i] in the alphabet is always the same as s[i+1] or before s[i+1].

Example 1:

Input: n = 1
Output: 5
Explanation: The 5 lexicographic strings consisting of vowels only are ["a", "e", "i", "o", "u"]
Example 2:

Input: n = 2
Output: 15
Explanation: The 15 lexicographic strings consisting 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 string that matches the meaning of the question, because 'e ' is later in the alphabet than 'a'
Example 3:

Input: n = 33
Output: 66045

hint:

1 <= n <= 50

Link: https://leetcode-cn.com/problems/count-sorted-vowel-strings

Thought analysis:

The first state:
dp[i][j] represents the total number of solutions with length i and ending with j, then the return value is dp[n][0…4]
Assuming the length is 2, ending with i, there can be ai, ei, ii,
then dp[i][j] = dp[i-1][0…j].
Initial state: dp[1][0…4] = 1

class Solution {
    
    
public:
    int countVowelStrings(int n) {
    
    
        int dp[n+1][5], ans = 0;
        memset(dp,0,sizeof(dp));
        for(int i = 0;i < 5;i++) dp[1][i] = 1;
        if(n == 1) return 5;
        for(int i = 2;i <= n;i++)
        {
    
    
            for(int j = 0;j < 5;j++)
            {
    
    
                for(int k = 0;k <= j;k++)
                {
    
    
                    dp[i][j] += dp[i-1][k];
                }
                if(i == n) ans += dp[i][j];
            }
        }
        return ans;
    }
};

For such code, the time complexity is O(n^3), which is not the most convenient, because there are some places that can be optimized.
We observe that dp[i][j] = dp[i-1][0…j], [0…j] is achieved by using one more loop. But this is actually the prefix sum. We can define a sum[i][j] to represent the sum of dp[i][0...j], and finally return sum[n][4].
Then dp[i][j] = sum[i-1][ j], sum[i][j] = sum[i][j-1]+dp[i][j]. Update the prefix sum of dp while updating the current dp.

class Solution {
    
    
public:
    int countVowelStrings(int n) {
    
    
        int dp[n+1][5], ans = 0, sum[n+1][5];
        memset(dp,0,sizeof(dp));
        memset(sum,0,sizeof(sum));
        for(int i = 0;i < 5;i++) dp[1][i] = 1;
        sum[1][0] = 1;
        for(int i = 1;i < 5;i++) sum[1][i] = sum[1][i-1]+dp[1][i-1];
        if(n == 1) return 5;
        for(int i = 2;i <= n;i++)
        {
    
    
            for(int j = 0;j < 5;j++)
            {
    
    
                dp[i][j] = sum[i-1][j];
                if(j == 0) sum[i][j] = dp[i][j];
                else sum[i][j] = sum[i][j-1]+dp[i][j];
            }
        }
        return sum[n][4];
    }
};

Optimize again. In fact, it may not be necessary for auxiliary arrays such as sum.
We define the state as dp[i][j] representing the total number of solutions of length i, [0…j], and finally find dp[n][4].
For example ["aa","ae","ai","ao","au","ee","ei","eo","eu","ii","io","iu","oo","ou","uu"]analysis:
assuming i = 2, j = 2, The results are: ai,ei,ii,aa,ae,ee.
We found that ai, ei, ii are dp[i-1][2], aa is dp[i-1][0], ae, ee are dp[i][1].
Then the state transition The equation can be written as:
dp[i][j] = dp[i-1][j]+dp[i][j-1].

//dp[i][j]表示长度为i,[0...j]的方案总数,最终求dp[n][4]
//dp[i][j] = dp[i-1][j] + dp[i][j-1]
//初始化:dp[0][0...4] = 1;

class Solution {
    
    
public:
    int countVowelStrings(int n) {
    
    
        int dp[n+1][5];
        memset(dp,0,sizeof(dp));
        for(int i = 0;i < 5;i++) dp[0][i] = 1;
        for(int i = 1;i <= n;i++)
        {
    
    
            for(int j = 0;j < 5;j++)
            {
    
    
                dp[i][j] = dp[i-1][j];
                if(j > 0) dp[i][j] += dp[i][j-1];
            }
        }
        return dp[n][4];
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325565699&siteId=291194637