Count Sorted Vowel Strings - Problem

Imagine you're creating a special dictionary where all words must follow very specific rules! ๐Ÿ“–

Given an integer n, you need to find how many strings of length n can be formed using only vowels (a, e, i, o, u) where the vowels appear in lexicographically sorted order.

What does lexicographically sorted mean? It means each character must be the same as or come before the next character in alphabetical order. For example:

  • โœ… "aei" is valid (a โ‰ค e โ‰ค i)
  • โœ… "aaa" is valid (all same characters)
  • โŒ "aie" is invalid (i > e)

Goal: Count all possible valid vowel strings of length n.

Example: For n=2, valid strings are: "aa", "ae", "ai", "ao", "au", "ee", "ei", "eo", "eu", "ii", "io", "iu", "oo", "ou", "uu" โ†’ Total: 15

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 1
โ€บ Output: 5
๐Ÿ’ก Note: For n=1, we can have 5 single-character strings: "a", "e", "i", "o", "u". All are lexicographically sorted by definition.
example_2.py โ€” Medium Case
$ Input: n = 2
โ€บ Output: 15
๐Ÿ’ก Note: Valid strings: "aa", "ae", "ai", "ao", "au" (5 starting with 'a'), "ee", "ei", "eo", "eu" (4 starting with 'e'), "ii", "io", "iu" (3 starting with 'i'), "oo", "ou" (2 starting with 'o'), "uu" (1 starting with 'u'). Total: 5+4+3+2+1 = 15.
example_3.py โ€” Larger Case
$ Input: n = 33
โ€บ Output: 66045
๐Ÿ’ก Note: For larger n, the pattern continues exponentially. Each position allows us to choose from remaining vowels, creating a combinatorial explosion that our DP solution handles efficiently.

Constraints

  • 1 โ‰ค n โ‰ค 50
  • Answer will fit in a 32-bit integer
  • Time limit: 1 second per test case

Visualization

Tap to expand
Building Vowel Strings (n=3)Foundation (Length 1)aeiouLevel 2 (Length 2)54321Level 3 (Length 3)1510631Building Rules ๐Ÿ“โ€ข Foundation: Any vowel can start (a, e, i, o, u)โ€ข Level 2+: Can only use vowels โ‰ฅ previous level's vowelโ€ข Count: Each cell shows # of valid towers ending with that vowelโ€ข Update: dp[vowel] += all dp[vowels_to_right] from previous level๐ŸŽฏ Final AnswerTotal valid strings of length 3: 15 + 10 + 6 + 3 + 1 = 35Time: O(n) | Space: O(1) | Perfect for interview!
Understanding the Visualization
1
Start with foundation
At length 1, we can use any of the 5 vowels as our foundation
2
Build upwards with restrictions
Each new level can only use vowels >= the previous level
3
Count all valid towers
DP efficiently counts all possible tower configurations
4
Sum the possibilities
Final answer is the total number of valid n-height towers
Key Takeaway
๐ŸŽฏ Key Insight: The lexicographic constraint creates a natural DP structure where each vowel choice restricts future options, leading to optimal substructure that we can solve bottom-up efficiently!
Asked in
Google 42 Amazon 38 Microsoft 31 Meta 25 Apple 18
78.3K Views
Medium-High Frequency
~12 min Avg. Time
2.8K 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