<p>Given an integer <code>n</code>, your task is to count how many strings of length <code>n</code> can be formed under the following rules:</p>

<ul>
	<li>Each character is a lower case vowel&nbsp;(<code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, <code>&#39;u&#39;</code>)</li>
	<li>Each vowel&nbsp;<code>&#39;a&#39;</code> may only be followed by an <code>&#39;e&#39;</code>.</li>
	<li>Each vowel&nbsp;<code>&#39;e&#39;</code> may only be followed by an <code>&#39;a&#39;</code>&nbsp;or an <code>&#39;i&#39;</code>.</li>
	<li>Each vowel&nbsp;<code>&#39;i&#39;</code> <strong>may not</strong> be followed by another <code>&#39;i&#39;</code>.</li>
	<li>Each vowel&nbsp;<code>&#39;o&#39;</code> may only be followed by an <code>&#39;i&#39;</code> or a&nbsp;<code>&#39;u&#39;</code>.</li>
	<li>Each vowel&nbsp;<code>&#39;u&#39;</code> may only be followed by an <code>&#39;a&#39;</code>.</li>
</ul>

<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;<code>10^9 + 7</code>.</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> All possible strings are: &quot;a&quot;, &quot;e&quot;, &quot;i&quot; , &quot;o&quot; and &quot;u&quot;.
</pre>

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

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 10
<strong>Explanation:</strong> All possible strings are: &quot;ae&quot;, &quot;ea&quot;, &quot;ei&quot;, &quot;ia&quot;, &quot;ie&quot;, &quot;io&quot;, &quot;iu&quot;, &quot;oi&quot;, &quot;ou&quot; and &quot;ua&quot;.
</pre>

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

<pre>
<strong>Input:</strong> n = 5
<strong>Output:</strong> 68</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 2 * 10^4</code></li>
</ul>

        // Cases - just write down using recursion 
        // Case 1. Each vowel 'a' may only be followed by an 'e'.
        // Case 2. Each vowel 'e' may only be followed by an 'a' or an 'i'.
        // Case 3. Each vowel 'i' may not be followed by another 'i'.
        // Case 4. Each vowel 'o' may only be followed by an 'i' or a 'u'.
        // Case 5. Each vowel 'u' may only be followed by an 'a'.
class Solution {
private: 
  int mod = 1e9+7;
   long long int solve(int index, char ch, int n,vector<vector<int>> & dp, unordered_map<char,int> & mp){
    
     if(index == n) 
           return 1;

     long long countVowels = 0;

     if(dp[mp[ch]][index] != -1){
        return dp[mp[ch]][index] % mod;
     }

     // power of recursive TREE
     // Recursive leaf of faith on the basic of conditions according to question
     if(ch == 'a'){
        countVowels += solve(index+1,'e',n,dp,mp);
     }else if(ch == 'e'){
        countVowels += solve(index+1,'a',n,dp,mp);
        countVowels += solve(index+1,'i',n,dp,mp);
     }else if(ch == 'i'){
        countVowels += solve(index+1,'a',n,dp,mp);
        countVowels += solve(index+1,'e',n,dp,mp);
        countVowels += solve(index+1,'o',n,dp,mp);
        countVowels += solve(index+1,'u',n,dp,mp);
     }else if(ch == 'o'){
        countVowels += solve(index+1,'i',n,dp,mp);
        countVowels += solve(index+1,'u',n,dp,mp);
     }else{
        countVowels += solve(index+1,'a',n,dp,mp);
     }
     return dp[mp[ch]][index] = countVowels % mod;
    }
public:
    int countVowelPermutation(int n) {
    
    // map to convert char to int for using in dp vector for memoization purpose
        unordered_map<char, int> mp;
        mp['a'] = 0;
        mp['e'] = 1;
        mp['i'] = 2;
        mp['o'] = 3;
        mp['u'] = 4;
        
        vector<vector<int>> dp(5, vector<int>(n+1, -1));

        // Try all possible ways (a,e,i,o,u)
        long long allPossibleWays =  
        solve(1,'a',n,dp,mp) + solve(1,'e',n,dp,mp) + solve(1,'i',n,dp,mp) + solve(1,'o',n,dp,mp)
                        + solve(1,'u',n,dp,mp);

        // Since the answer may be too large, return it modulo 10^9 + 7.
        return allPossibleWays % mod;
    }
};
22495