<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>

class Solution:

    # DP
    def countVowelPermutation(self, n: int) -> int:
        MOD = 10**9 + 7 

        @cache
        def dp(i, j):

            if i <= 1:
                return i

            if i == n+1:
                all_sums = [ dp(n, vowel) for vowel in range(5) ]
                return sum(all_sums) % MOD

            prev_candidates = 'aeiou'

            # Rules
            if j == 0:  # a
                prev_candidates = [1, 2, 4] #'eiu'
            elif j == 1:  # e
                prev_candidates = [0, 2] #'ai'
            elif j == 2:  # i
                prev_candidates = [1, 3] #'eo'
            elif j == 3:  # o
                prev_candidates = [2] #'i'
            elif j == 4:  # u
                prev_candidates = [2,3] #'io'

            all_sums = [ dp(i-1, vowel) for vowel in prev_candidates ]
            return sum(all_sums)

        return dp(n+1, None)

    # MLE 5/64  
    #   n = 144
    #   I know keeping answers will not be ok.
    #   Trying not checking uniqueness.. 
    # Good until ~20.
    #   After read hints.... DP!
    def countVowelPermutation_BACKTRACK(self, n: int) -> int:
        global count

        MOD = 10**9 + 7 

        # answers = set()
        count = 0
        curr_str = []

        def backtrack():
            global count

            if len(curr_str) == n:
                # answers.add(tuple(curr_str))
                count = (count + 1) % MOD
                return

            next_valid = 'aeiou'

            # Rules
            if curr_str:

                prev = curr_str[-1]

                if prev == 'a':
                    next_valid = 'e'
                elif prev == 'e':
                    next_valid = 'ai'
                elif prev == 'i':
                    next_valid = 'aeou'
                elif prev == 'o':
                    next_valid = 'iu'
                else:
                    next_valid = 'a'

            for c in next_valid:

                curr_str.append(c)
                backtrack()
                curr_str.pop()

        backtrack()
        
        return count
174522