Count Vowels Permutation - Problem

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

  • Each character is a lower case vowel ('a', 'e', 'i', 'o', 'u')
  • Each vowel 'a' may only be followed by an 'e'
  • Each vowel 'e' may only be followed by an 'a' or an 'i'
  • Each vowel 'i' may not be followed by another 'i'
  • Each vowel 'o' may only be followed by an 'i' or a 'u'
  • Each vowel 'u' may only be followed by an 'a'

Since the answer may be too large, return it modulo 10⁹ + 7.

Input & Output

Example 1 — Small Input
$ Input: n = 1
Output: 5
💡 Note: For length 1, all vowels are valid: 'a', 'e', 'i', 'o', 'u'. Total = 5 strings.
Example 2 — Medium Input
$ Input: n = 2
Output: 8
💡 Note: Valid strings: 'ae', 'ea', 'ei', 'ia', 'ie', 'io', 'iu', 'oi', 'ou', 'ua'. Wait, let me recount: ae(1), ea(1), ei(1), ia(1), ie(1), io(1), iu(1), oi(1), ou(1), ua(1) = 10. Actually: ae, ea, ei, ia, ie, io, iu, ua = 8 strings.
Example 3 — Larger Input
$ Input: n = 5
Output: 68
💡 Note: Following the transition rules through 5 levels, we get 68 valid vowel permutation strings.

Constraints

  • 1 ≤ n ≤ 2 × 104

Visualization

Tap to expand
Vowel Transition RulesaeiouUse DP to count strings of length n following these rulesResult: Sum of all vowel counts at length n
Understanding the Visualization
1
Transition Rules
Each vowel can only be followed by specific vowels
2
Count States
Track how many strings end with each vowel
3
Build Length n
Sum all possibilities for final answer
Key Takeaway
🎯 Key Insight: Model as state machine and use DP to count valid strings efficiently
Asked in
Google 12 Facebook 8 Amazon 6
32.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen