-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcountSortedVowelStrings.py
102 lines (81 loc) · 2.1 KB
/
countSortedVowelStrings.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# https://leetcode.com/problems/count-sorted-vowel-strings/
class Solution:
# HEHE
def countVowelStrings(self, n: int) -> int:
result = [
5,
15,
35,
70,
126,
210,
330,
495,
715,
1001,
1365,
1820,
2380,
3060,
3876,
4845,
5985,
7315,
8855,
10626,
12650,
14950,
17550,
20475,
23751,
27405,
31465,
35960,
40920,
46376,
52360,
58905,
66045,
73815,
82251,
91390,
101270,
111930,
123410,
135751,
148995,
163185,
178365,
194580,
211876,
230300,
249900,
270725,
292825,
316251
]
return result[n-1]
# Generating above sequence using formula n = n*(n-1)*(n-2)*(n-3) //24
def countVowelStrings(self, n: int) -> int:
return [(i*(i-1)*(i-2)*(i-3))//24 for i in range (5, 56)][n-1]
# Recursion / Brute Force
def countVowelStrings(self, n: int) -> int:
array = ["a","e","i","o","u"]
self.result =[]
def helper(n, s, index):
if n==0:
self.result.append(s)
return
for i in range(index, len(array)):
helper(n-1, s+array[i], i)
return self.result
return len(helper(n, "", 0))
# Expected Solution
# Runtime: 33 ms, faster than 80.43% of Python3 online submissions for Count Sorted Vowel Strings.
# Memory Usage: 13.9 MB, less than 27.45% of Python3 online submissions for Count Sorted Vowel Strings.
def countVowelStrings(self, n: int) -> int:
array = [1,1,1,1,1]
for i in range (n):
for j in range(3, -1, -1):
array[j]+=array[j+1]
return sum(array)