LeetCode 1220. Count Vowels Permutation Shell Solution | Explanation + Code

CoderIndeed
0
1220. Count Vowels Permutation

Description

Given an integer n, your task is to count how many strings of length n can be formed under the following rules:

  • Each character is a lower case vowel ('a', 'e', 'i', 'o', 'u')
  • Each vowel 'a' may only be followed by an 'e'.
  • Each vowel 'e' may only be followed by an 'a' or an 'i'.
  • Each vowel 'i' may not be followed by another 'i'.
  • Each vowel 'o' may only be followed by an 'i' or a 'u'.
  • Each vowel 'u' may only be followed by an 'a'.

Since the answer may be too large, return it modulo 10^9 + 7.

 

Example 1:

Input: n = 1
Output: 5
Explanation: All possible strings are: "a", "e", "i" , "o" and "u".

Example 2:

Input: n = 2
Output: 10
Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".

Example 3: 

Input: n = 5
Output: 68

 

Constraints:

  • 1 <= n <= 2 * 10^4

Solutions

Solution 1: Dynamic Programming

Based on the problem description, we can list the possible subsequent vowels for each vowel:

a [e]
e [a|i]
i [a|e|o|u]
o [i|u]
u [a]

From this, we can deduce the possible preceding vowels for each vowel:

[e|i|u]	a
[a|i]	e
[e|o]	i
[i]	o
[i|o]	u

We define f[i] as the number of strings of the current length ending with the i-th vowel. If the length is 1, then f[i]=1.

When the length is greater than 1, we define g[i] as the number of strings of the current length ending with the i-th vowel. Then g[i] can be derived from f, that is:

g[i]= \begin{cases} f[1]+f[2]+f[4] & i=0 \ f[0]+f[2] & i=1 \ f[1]+f[3] & i=2 \ f[2] & i=3 \ f[2]+f[3] & i=4 \end{cases}

The final answer is ∑_{i=0}4f[i]. Note that the answer may be very large, so we need to take the modulus of 109+7.

The time complexity is O(n), and the space complexity is O(C). Here, n is the length of the string, and C is the number of vowels. In this problem, C=5.

PythonJavaC++GoTypeScriptJavaScript
class Solution: def countVowelPermutation(self, n: int) -> int: f = [1] * 5 mod = 10**9 + 7 for _ in range(n - 1): g = [0] * 5 g[0] = (f[1] + f[2] + f[4]) % mod g[1] = (f[0] + f[2]) % mod g[2] = (f[1] + f[3]) % mod g[3] = f[2] g[4] = (f[2] + f[3]) % mod f = g return sum(f) % mod(code-box)

Solution 2: Matrix Exponentiation to Accelerate Recursion

The time complexity is O(C3 × log n), and the space complexity is O(C2). Here, C is the number of vowels. In this problem, C=5.

PythonJavaC++GoTypeScriptJavaScript
import numpy as np class Solution: def countVowelPermutation(self, n: int) -> int: mod = 10**9 + 7 factor = np.asmatrix( [ (0, 1, 0, 0, 0), (1, 0, 1, 0, 0), (1, 1, 0, 1, 1), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), ], np.dtype("O"), ) res = np.asmatrix([(1, 1, 1, 1, 1)], np.dtype("O")) n -= 1 while n: if n & 1: res = res * factor % mod factor = factor * factor % mod n >>= 1 return res.sum() % mod(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !