File tree 6 files changed +161
-4
lines changed
solution/1600-1699/1641.Count Sorted Vowel Strings
6 files changed +161
-4
lines changed Original file line number Diff line number Diff line change 45
45
<li><code>1 <= n <= 50</code> </li>
46
46
</ul >
47
47
48
-
49
48
## 解法
50
49
51
50
<!-- 这里可写通用的实现逻辑 -->
52
51
52
+ ```
53
+ a e i o u
54
+ 1 1 1 1 1 n=1
55
+ 5 4 3 2 1 n=2
56
+ 15 10 6 3 1 n=3
57
+ ... n=...
58
+ ```
59
+
53
60
<!-- tabs:start -->
54
61
55
62
### ** Python3**
56
63
57
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
65
59
66
``` python
60
-
67
+ class Solution :
68
+ def countVowelStrings (self , n : int ) -> int :
69
+ cnt = [1 ] * 5
70
+ for i in range (2 , n + 1 ):
71
+ for j in range (3 , - 1 , - 1 ):
72
+ cnt[j] += cnt[j + 1 ]
73
+ return sum (cnt)
61
74
```
62
75
63
76
### ** Java**
64
77
65
78
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
79
67
80
``` java
81
+ class Solution {
82
+ public int countVowelStrings (int n ) {
83
+ int [] cnt = new int [5 ];
84
+ Arrays . fill(cnt, 1 );
85
+ for (int i = 2 ; i <= n; ++ i) {
86
+ for (int j = 3 ; j >= 0 ; -- j) {
87
+ cnt[j] += cnt[j + 1 ];
88
+ }
89
+ }
90
+ return Arrays . stream(cnt). sum();
91
+ }
92
+ }
93
+ ```
94
+
95
+ ### ** C++**
96
+
97
+ ``` cpp
98
+ class Solution {
99
+ public:
100
+ int countVowelStrings(int n) {
101
+ vector<int > cnt(5, 1);
102
+ for (int i = 2; i <= n; ++i)
103
+ for (int j = 3; j >= 0; --j)
104
+ cnt[ j] += cnt[ j + 1] ;
105
+ return accumulate(cnt.begin(), cnt.end(), 0);
106
+ }
107
+ };
108
+ ```
68
109
110
+ ### **Go**
111
+
112
+ ```go
113
+ func countVowelStrings(n int) int {
114
+ cnt := make([]int, 5)
115
+ for i := range cnt {
116
+ cnt[i] = 1
117
+ }
118
+ for i := 2; i <= n; i++ {
119
+ for j := 3; j >= 0; j-- {
120
+ cnt[j] += cnt[j+1]
121
+ }
122
+ }
123
+ ans := 0
124
+ for _, v := range cnt {
125
+ ans += v
126
+ }
127
+ return ans
128
+ }
69
129
```
70
130
71
131
### ** ...**
Original file line number Diff line number Diff line change @@ -41,21 +41,73 @@ Note that "ea" is not a valid string since 'e' comes after 
41
41
<li><code>1 <= n <= 50</code> </li>
42
42
</ul >
43
43
44
-
45
44
## Solutions
46
45
47
46
<!-- tabs:start -->
48
47
49
48
### ** Python3**
50
49
51
50
``` python
52
-
51
+ class Solution :
52
+ def countVowelStrings (self , n : int ) -> int :
53
+ cnt = [1 ] * 5
54
+ for i in range (2 , n + 1 ):
55
+ for j in range (3 , - 1 , - 1 ):
56
+ cnt[j] += cnt[j + 1 ]
57
+ return sum (cnt)
53
58
```
54
59
55
60
### ** Java**
56
61
57
62
``` java
63
+ class Solution {
64
+ public int countVowelStrings (int n ) {
65
+ int [] cnt = new int [5 ];
66
+ Arrays . fill(cnt, 1 );
67
+ for (int i = 2 ; i <= n; ++ i) {
68
+ for (int j = 3 ; j >= 0 ; -- j) {
69
+ cnt[j] += cnt[j + 1 ];
70
+ }
71
+ }
72
+ return Arrays . stream(cnt). sum();
73
+ }
74
+ }
75
+ ```
76
+
77
+ ### ** C++**
78
+
79
+ ``` cpp
80
+ class Solution {
81
+ public:
82
+ int countVowelStrings(int n) {
83
+ vector<int > cnt(5, 1);
84
+ for (int i = 2; i <= n; ++i)
85
+ for (int j = 3; j >= 0; --j)
86
+ cnt[ j] += cnt[ j + 1] ;
87
+ return accumulate(cnt.begin(), cnt.end(), 0);
88
+ }
89
+ };
90
+ ```
58
91
92
+ ### **Go**
93
+
94
+ ```go
95
+ func countVowelStrings(n int) int {
96
+ cnt := make([]int, 5)
97
+ for i := range cnt {
98
+ cnt[i] = 1
99
+ }
100
+ for i := 2; i <= n; i++ {
101
+ for j := 3; j >= 0; j-- {
102
+ cnt[j] += cnt[j+1]
103
+ }
104
+ }
105
+ ans := 0
106
+ for _, v := range cnt {
107
+ ans += v
108
+ }
109
+ return ans
110
+ }
59
111
```
60
112
61
113
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int countVowelStrings (int n) {
4
+ vector<int > cnt (5 , 1 );
5
+ for (int i = 2 ; i <= n; ++i)
6
+ for (int j = 3 ; j >= 0 ; --j)
7
+ cnt[j] += cnt[j + 1 ];
8
+ return accumulate (cnt.begin (), cnt.end (), 0 );
9
+ }
10
+ };
Original file line number Diff line number Diff line change
1
+ func countVowelStrings (n int ) int {
2
+ cnt := make ([]int , 5 )
3
+ for i := range cnt {
4
+ cnt [i ] = 1
5
+ }
6
+ for i := 2 ; i <= n ; i ++ {
7
+ for j := 3 ; j >= 0 ; j -- {
8
+ cnt [j ] += cnt [j + 1 ]
9
+ }
10
+ }
11
+ ans := 0
12
+ for _ , v := range cnt {
13
+ ans += v
14
+ }
15
+ return ans
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countVowelStrings (int n ) {
3
+ int [] cnt = new int [5 ];
4
+ Arrays .fill (cnt , 1 );
5
+ for (int i = 2 ; i <= n ; ++i ) {
6
+ for (int j = 3 ; j >= 0 ; --j ) {
7
+ cnt [j ] += cnt [j + 1 ];
8
+ }
9
+ }
10
+ return Arrays .stream (cnt ).sum ();
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countVowelStrings (self , n : int ) -> int :
3
+ cnt = [1 ] * 5
4
+ for i in range (2 , n + 1 ):
5
+ for j in range (3 , - 1 , - 1 ):
6
+ cnt [j ] += cnt [j + 1 ]
7
+ return sum (cnt )
You can’t perform that action at this time.
0 commit comments