Count Vowels Permutation solution explained

Count Vowels Permutation asks how many strings of length n can be built from a, e, i, o, u when each vowel limits which vowel may come next. Keep five counters, the number of valid strings ending in each vowel, and extend them one letter at a time using the rules read backwards: a new a can follow e, i or u, a new e can follow a or i, and so on. That is O(n) time and O(1) memory, with the total reported modulo 1,000,000,007.

By Published 6 min read

The problem in plain words

Build a string of exactly n letters, every letter a lowercase vowel. Adjacent letters must follow these rules: after an a, only an e is allowed; after an e, an a or an i; after an i, any vowel except another i; after an o, an i or a u; after a u, only an a. The first letter can be any vowel. Count the strings that satisfy every rule. The count grows by a factor of nearly 2 per letter, so report it modulo 1,000,000,007.

The rules both ways round (the test derives the right column from the left)
VowelMay be followed byMay be preceded by
aee, i, u
ea, ia, i
ia, e, o, ue, o
oi, ui
uai, o
The rules both ways round (the test derives the right column from the left). Scroll sideways on a small screen.
Counts from the code on this page, matched by brute force up to n = 8
nStringsnStrings
156129
2107249
3198474
435101,739
Counts from the code on this page, matched by brute force up to n = 8. Scroll sideways on a small screen.

At length 2 you can list them: ae, ea, ei, ia, ie, io, iu, oi, ou, ua. That is one pair per arrow in the 'may be followed by' column, ten in all.

What the interviewer is checking

The question checks whether you can model a sequence rule as a small state machine and count paths through it. The interviewer listens for the state (length so far and the last letter), a correct transition table, and the realisation that only the previous length matters. Reversing the rule table correctly, or explaining why a forward push works just as well, is where candidates separate. A matrix-power remark for huge n is a welcome extra.

Brute force: build every string

Generate all 5^n strings and keep those where every neighbouring pair is allowed. It is a good sanity check and the reference used in testing, but 5^n outruns any time limit quickly. A recursive version that only extends valid prefixes is better, since it never builds an invalid string, but it still lists every valid one, and their number grows by about 1.9 per letter.

@@count-vowels-permutation/slow.py@@
Python: enumerate all strings and check each pair.

The pattern: counts per last letter

What can come next depends only on the last letter, so group the strings by their last letter and keep a count for each group. Start with one string of length 1 ending in each vowel. To get the counts for the next length, ask for each vowel: which groups can it be appended to? That is the 'may be preceded by' column. A new a can be appended to strings ending in e, i or u, so new_a = e + i + u. Doing all five at once, from the old values, gives the next row. After n - 1 rounds, add the five counters.

@@count-vowels-permutation/fast.py@@
Python: five counters updated together from the previous round.

There is an equally valid 'push' form that uses the forward rules directly: start each new counter at zero, then for each vowel X, add X's count to every vowel that may follow X. It touches the same ten arrows. Pick whichever direction you can state without hesitating, and say which one you chose.

In JavaScript, the destructuring assignment computes every right-hand value before assigning, which gives the 'update together' behaviour in one line. Each value is under the modulus, so the largest sum, three counters, stays near 3 billion, far below the 2^53 limit for exact integers.

@@count-vowels-permutation/fast.js@@
JavaScript: the same five counters with a simultaneous update.

Faster for enormous n

The update is linear, so it can be written as a 5 by 5 matrix whose entry (X, Y) is 1 when Y may follow X. The number of valid strings of length n is the sum of all entries of that matrix raised to the power n - 1, which repeated squaring computes in O(log n) small matrix products. It is the same move that speeds up Fibonacci, and worth a sentence if the interviewer asks about n in the billions. In a language with 64-bit floats, do those products with big integers or split multiplication, because two values near the modulus multiply to roughly 10^18.

Complexity

n is the string length
ApproachTimeExtra memory
Every string over five lettersO(5^n * n)O(n)
Extend only valid prefixesProportional to the answer, about 1.9^nO(n) recursion
Five countersO(n)O(1)
5 by 5 matrix powerO(log n) matrix productsO(1)
n is the string length. Scroll sideways on a small screen.

Edge cases

  • n = 1: every single vowel is valid, so 5; the loop runs zero times.
  • n = 2: ten strings, one per allowed pair.
  • Large n: take the remainder after every addition.
  • A variant that fixes the first or last letter: start only that counter at 1, or read only that counter at the end.

Typical mistakes

  • Copying the 'may follow' rules straight into the pull formula, so new_a becomes the count of e instead of e + i + u.
  • Updating the counters one by one in place, so later formulas read values from the new round.
  • Running n rounds instead of n - 1.
  • Leaving out the remainder until the end in Java or C++, which overflows long before n reaches a few hundred.
  • Treating 'i may not be followed by i' as 'i may only be followed by i', an easy misreading under pressure.

How to explain it out loud

Fictional example

Flipping the rule table on purpose
Candidate
What's allowed next only depends on the last letter, so I'll track five counts: strings of the current length ending in each vowel.
Candidate
To append, I need the rules the other way round. Which vowels can come right before an a? Only e, i and u point to a. Let me write that column out before coding.
Interviewer
Why compute all five at once?
Candidate
Each new count has to come from the previous length. If I updated a first and then used it for e, I'd be mixing two lengths. So a tuple assignment, n minus 1 rounds, then the sum.

Knight Dialer is the same counting-walks idea on ten keypad digits. Domino and Tromino Tiling also carries a small set of states from one step to the next. Fibonacci Number shows the matrix-power speed-up in its simplest form. Student Attendance Record II is a good next problem with the same structure and a slightly richer state.

Natively in your preparation

The bug in this problem is almost always a flipped rule, and catching it is easier with a second pair of eyes. When you practice on Mac or Windows, Natively can read the rules from a screenshot of the prompt, compare your 'preceded by' column with them, and ask you why the update must be simultaneous. The transcript from that session makes it easy to replay how you stated the state.

Questions people ask

How do you solve Count Vowels Permutation?

Keep five counters, the valid strings of the current length ending in a, e, i, o and u. Each round set new a = e + i + u, new e = a + i, new i = e + o, new o = i and new u = i + o, all from the old values, then sum after n - 1 rounds.

Why read the vowel rules backwards?

The rules say what may follow each vowel, but extending a string asks what may come before the new letter. Flipping the table gives, for each vowel, the vowels that can precede it.

What is the time complexity of Count Vowels Permutation?

O(n) time and O(1) memory with five counters. A 5 by 5 matrix power gives O(log n) matrix products for very large n.

How many valid strings of length 2 are there?

Ten: ae, ea, ei, ia, ie, io, iu, oi, ou and ua, one for each allowed pair in the rule table.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP