Wednesday 11 May 2022

Leetcode 1641. Count Sorted Vowel Strings

Problem Link

We have to get the number of permutations we can have with using 5 vowels character in lexicographical order.

After every vowel character we can only take characters which are lexicographically equal or larger than it.
Suppose, we have a substring "aei" but we have to make the string size 4. But we can't take character "a" or "e", we forced to take character "i", "o", or "u".
For decrease the time complexity we used memoization technique with some extra memory space.

Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    int memo[51][6];
    int solve(int n,int i){
        if(n==0){
            return 1;
        }
        if(memo[n][i]!=0){
            return memo[n][i];
        }
        int forReturn = 0;
        for(int j=i;j<5;j++){
            forReturn += solve(n-1,j);
        }
        memo[n][i] = forReturn;
        return memo[n][i];
    }
    int countVowelStrings(int n) {
        return solve(n,0);
    }
};

No comments:

Post a Comment

If you have any doubts, let me know through comments

Monkey Banana Problem lightoj 1004

  In this problem we will check which adjacent cell will benefits the monkey best. For this, we will check all possible solution of this pro...