Count Sorted Vowel Strings (LeetCode 1641): Solution and Explanation

Medium
Math
Dynamic Programming
Combinatorics

Count the strings of length n built from a, e, i, o, u whose letters never go backwards in the alphabet. For n = 2 there are 15 such strings, from aa to uu.

Intuition

A sorted string can only stay on its current vowel or move to a later one. So what matters about a string is the vowel it starts with: that decides which strings may follow it.

Let dp[v] be the number of sorted strings of the current length that start with vowel v. A string of length L that starts with v is v followed by any sorted string of length L - 1 that starts at v or later.

That makes the new dp[v] the sum of the old dp[v..u], a suffix sum. Sweeping from right to left in place, dp[v + 1] already holds the suffix sum for the later vowels, so one addition per cell is enough.

Approach: Suffix-sum DP on the starting vowel

  1. Start with dp = [1, 1, 1, 1, 1]: one string of length 1 per vowel.
  2. For each length from 2 to n, leave dp[u] alone (only uu...u starts with u).
  3. Sweep v from o down to a and do dp[v] += dp[v + 1].
  4. Return the sum of the five counts.

Step-by-step example

n = 1
  1. dp starts as [1, 1, 1, 1, 1]: the strings a, e, i, o and u.
  2. The loop over lengths 2..n does not run because n is 1.
  3. The answer is 1 + 1 + 1 + 1 + 1 = 5.
  4. For comparison, with n = 2 the sweep turns the row into [5, 4, 3, 2, 1], which sums to 15.

Code: Python, Java, C++

1class Solution:
2    def countVowelStrings(self, n: int) -> int:
3        dp = [1] * 5
4        for length in range(2, n + 1):
5            for v in range(3, -1, -1):
6                dp[v] += dp[v + 1]
7        return sum(dp)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each of the n - 1 lengths does four additions.

Space

O(1)

Only five counters are kept.

Other approaches

Combinatorics (time O(1), space O(1))

A sorted string is fixed by how many of each vowel it uses, so the answer is the number of ways to split n into 5 non-negative parts: C(n + 4, 4). It is O(1) but harder to come up with in an interview.

Backtracking (time O(n^4), space O(n))

Generate every sorted string by choosing each next vowel at or after the previous one. It is easy to write but makes about 316,000 strings for n = 50.

Common mistakes

  • Sweeping left to right. Then dp[v + 1] still holds the old value and the row becomes a prefix sum of the wrong thing.
  • Counting strings by the vowel they end with but still summing in the wrong direction. Either orientation works, as long as the sweep direction matches it.
  • Treating it as 5^n strings and filtering. That is far too many for n = 50.

Frequently asked questions

Why does the answer equal C(n + 4, 4)?

A sorted string is decided by the counts of a, e, i, o and u, which are five non-negative numbers adding up to n. Stars and bars gives C(n + 4, 4) ways to pick them.

Does the answer fit in a 32-bit integer?

Yes. For n = 50 it is C(54, 4) = 316,251.

Why must the update go from right to left?

The new value for v needs the new value for v + 1, which is the sum of all later vowels. Going right to left computes v + 1 first, so it is ready when v needs it.