738. Sorted Vowel Words

A museum audio guide uses short codes built from the five letters a, e, i, o and u. To keep codes easy to read aloud, the letters in a code must never go backwards in alphabetical order: each letter is either the same as the one before it or comes later in the sequence a, e, i, o, u. For instance aeeou is acceptable but eau is not.

Given the code length n, return how many different acceptable codes of exactly that length exist. Aim for time O(n) and extra space O(1).

Example 1

Input:
n = 3
Output:
35
Explanation:

By last letter the counts are 1, 3, 6, 10 and 15, which add up to 35 acceptable codes.

Example 2

Input:
n = 6
Output:
210
Explanation:

Codes of length six correspond to choosing how many of each letter appear, which gives 210 codes.

Example 3

Input:
n = 10
Output:
1001
Explanation:

Repeating the prefix-sum step up to length ten gives 1001 acceptable codes.

Constraints

1 ≤ n ≤ 100

The answer fits in a 32-bit signed integer.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…