Count Vowels Permutation LeetCode solution in Python

1
# Runtime: 249 ms (Top 78.97%) | Memory: 19.8 MB (Top 29.73%)
2
class Solution:
3
def countVowelPermutation(self, n: int) -> int:
4
# dp[i][j] means the number of strings of length i that ends with the j-th vowel.
5
dp = [[1] * 5] + [[0] * (5) for _ in range(n - 1)]
6
moduler = math.pow(10, 9) + 7
7
for i in range(1, n):
8
# For vowel a
9
dp[i][0] = (dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][4]) % moduler
10
# For vowel e
11
dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % moduler
12
# For vowel i
13
dp[i][2] = (dp[i - 1][1] + dp[i - 1][3]) % moduler
14
# For vowel o
15
dp[i][3] = (dp[i - 1][2]) % moduler
16
# For vowel u
17
dp[i][4] = (dp[i - 1][2] + dp[i - 1][3]) % moduler
18

19
return int(sum(dp[-1]) % moduler)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0