Problem 561374 · medium · Phase 05 Advanced Algorithms & Graphs

Ordered Vowel Chants

dynamic programming · 1-D dp · counting · non-decreasing sequences

A chant is a string of length n over the five vowels a e i o u. A chant is ordered if its letters never go backwards in the alphabet: every letter is the same as or later than the letter before it (so aeu and iii are ordered, ea is not). Return the number of ordered chants of length n. The empty chant (n = 0) counts as one ordered chant.

Examples

Input:  n = 1
Output: 5

Input:  n = 2
Output: 15
Explanation: aa ae ai ao au ee ei eo eu ii io iu oo ou uu.

Constraints

  • 0 <= n <= 10**5
  • Target complexity: O(n) time; the exact count is required (Python integers are unbounded).

Goals

  • Count sorted strings with a small fixed-size state per position
  • Use suffix sums so each step costs constant work
Starting Python…