<p>Given an integer <code>n</code>, return <em>the number of strings of length </em><code>n</code><em> that consist only of vowels (</em><code>a</code><em>, </em><code>e</code><em>, </em><code>i</code><em>, </em><code>o</code><em>, </em><code>u</code><em>) and are <strong>lexicographically sorted</strong>.</em></p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> The 5 sorted strings that consist of vowels only are <code>[&quot;a&quot;,&quot;e&quot;,&quot;i&quot;,&quot;o&quot;,&quot;u&quot;].</code>
</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 33
<strong>Output:</strong> 66045
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 50</code>&nbsp;</li>
</ul>

// Class to implement the Top-down approach:
class Solution {
    vector<char> vowels = {'a','e','i','o','u'};

public:
    // Method to find the number of such strings of length n, using recursion with memoization - O(N) & O(N)
    int countVowelStrings(int n) {
        vector<vector<int>> memory(n + 1, vector<int>(26, -1));
        return solveWithMemo(memory, n, 'w');
    }

private:
    // O(5*N*26) & O(N*26 + N)
    int solveWithMemo(vector<vector<int>>& memory, int n, int prevLetter) {
        // Edge case: If you've created such string of length n then return 1
        if(n == 0)
            return 1;
        
        // Memoization table: If the current state is already computed then return the computed value
        if(memory[n][prevLetter - 'a'] != -1)
            return memory[n][prevLetter - 'a'];
        
        // Stores the result value
        int count = 0;

        // Explore all the ways of creating such string from the vowels
        for(char v : vowels) 
            if(prevLetter == 'w' || prevLetter <= v) 
                count += solveWithMemo(memory, n - 1, v);

        // Store the result value to the memoization table and then return it
        return memory[n][prevLetter - 'a'] = count;
    }

    // O(5^N) & O(N)
    int solveWithoutMemo(int n, int prevLetter) {
        // Edge case: If you've created such string of length n then return 1
        if(n == 0)
            return 1;
        
        // Stores the result value
        int count = 0;

        // Explore all the ways of creating such string from the vowels
        for(char v : vowels) 
            if(prevLetter == 'w' || prevLetter <= v) 
                count += solveWithoutMemo(n - 1, v);

        // Return the result value
        return count;
    }
};
8200