↓Skip to main content
  1. LeetCode/

LeetCode 1220: Count Vowels Permutation

·2 mins· ·
LeetCode Hard Dynamic-Programming State-Machine
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-10-15

  • Submissions: Wrong Answer ×2 → Accepted

Approach
#

The state is the last vowel, with a, e, i, o, u counting strings ending in it. The problem says “what may follow x”, so for transitions read it backwards as “what may precede x”:

Ends withPrevious may be
ae, i, u
ea, i
ie, o
oi
ui, o

At length 1 each is 1; apply n - 1 transitions.

My Attempts
#

  1. Attempt 1: the modulus was 10 % 9 + 7 (which is 8) instead of 10 ** 9 + 7, and the final sum had no modulo. For n = 5 the answer is 68; it returned 20.
  2. Attempt 2: added the final modulo, but the modulus was still 8. For n = 2 the answer is 10; it returned 10 % 8 = 2.
  3. Changing it to 10 ** 9 + 7 passed.

Solution
#

class Solution:
    def countVowelPermutation(self, n: int) -> int:
        MOD = 10 ** 9 + 7
        a = e = i = o = u = 1
        for _ in range(n - 1):
            a, e, i, o, u = (
                (e + i + u) % MOD,                 # previous may be e, i, u
                (a + i) % MOD,
                (e + o) % MOD,
                i % MOD,
                (i + o) % MOD,
            )
        return (a + e + i + o + u) % MOD

Simultaneous assignment avoids separate prev_a-style variables.

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • 10 % 9 + 7 and 10 ** 9 + 7 differ by one character; small tests pass and only large ones fail.
  • The rules say what follows x, so the transitions must be reversed.

Interview Explanation
#

The state is the last vowel, with five variables counting strings ending in each. The problem gives what may follow each vowel, so I reverse it to what may precede each, update all five at once each step, and sum with the modulo at the end. Time O(n).

Related

LeetCode 2912: Number of Ways to Reach Destination in the Grid
·3 mins
LeetCode Hard Dynamic-Programming State-Machine Math Combinatorics
LeetCode 2912 note: many cells, but only four classes relative to the destination (itself, same row, same column, neither); each class’s total moves together in O(k)
LeetCode 1411: Number of Ways to Paint N × 3 Grid
·2 mins
LeetCode Hard Dynamic-Programming State-Machine Graph-Coloring
LeetCode 1411 note: a row’s colorings come in two patterns, ABA (ends match) and ABC (all different), 6 each; the next row gives ABA → 3 ABA + 2 ABC and ABC → 2 ABA + 2 ABC
LeetCode 1320: Minimum Distance to Type a Word Using Two Fingers
·2 mins
LeetCode Hard Dynamic-Programming State-Machine String
LeetCode 1320 note: after typing word[i], one finger is always on word[i], so the state is only the other finger’s position; dp[other] is the least distance with the other finger at other
LeetCode 552: Student Attendance Record II
·2 mins
LeetCode Hard Dynamic-Programming State-Machine
LeetCode 552 note: state machine DP with state “A used × trailing L run”, 6 states; each day’s P, A or L picks the next state
LeetCode 123: Best Time to Buy and Sell Stock III
·4 mins
LeetCode Hard Dynamic-Programming State-Machine
LeetCode note for Best Time to Buy and Sell Stock III, rebuilt from the original learning note
LeetCode 188: Best Time to Buy and Sell Stock IV
·4 mins
LeetCode Hard Dynamic-Programming State-Machine
LeetCode note for Best Time to Buy and Sell Stock IV, rebuilt from the original learning note