forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1220-Count-Vowels-Permutation.py
39 lines (39 loc) · 1.75 KB
/
1220-Count-Vowels-Permutation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution:
Memo = {}
def countVowelPermutation(self, n, c = '') -> int:
if (c, n) in self.Memo:
return self.Memo[(c, n)]
if n == 1:
if c == 'a':
return 1
if c == 'e':
return 2
if c == 'i':
return 4
if c == 'o':
return 2
if c == 'u':
return 1
if c == '':
return 5
else:
if c == 'a':
self.Memo[('a', n)] = self.countVowelPermutation(n - 1, 'e')
return self.Memo[('a', n)]
if c == 'e':
self.Memo[('e', n)] = self.countVowelPermutation(n - 1, 'a') + self.countVowelPermutation(n - 1, 'i')
return self.Memo[('e', n)]
if c == 'i':
self.Memo[('i', n)] = self.countVowelPermutation(n - 1, 'a') + self.countVowelPermutation(n - 1, 'e') + self.countVowelPermutation(n - 1, 'o') + self.countVowelPermutation(n - 1, 'u')
return self.Memo[('i', n)]
if c == 'o':
self.Memo[('o', n)] = self.countVowelPermutation(n - 1, 'i') + self.countVowelPermutation(n - 1, 'u')
return self.Memo[('o', n)]
if c == 'u':
self.Memo[('u', n)] = self.countVowelPermutation(n - 1, 'a')
return self.Memo[('u', n)]
if c == '':
Tot = 0
for i in ['a', 'e', 'i', 'o', 'u']:
Tot = Tot + self.countVowelPermutation(n - 1, i);
return Tot % 1000000007