File tree 3 files changed +110
-0
lines changed
solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length
3 files changed +110
-0
lines changed Original file line number Diff line number Diff line change @@ -203,6 +203,45 @@ function maxVowels(s: string, k: number): number {
203
203
}
204
204
```
205
205
206
+ ### ** PHP**
207
+
208
+ ``` php
209
+ class Solution {
210
+ /**
211
+ * @param String $s
212
+ * @param Integer $k
213
+ * @return Integer
214
+ */
215
+ function isVowel($c) {
216
+ return $c === 'a' ||
217
+ $c === 'e' ||
218
+ $c === 'i' ||
219
+ $c === 'o' ||
220
+ $c === 'u';
221
+ }
222
+ function maxVowels($s, $k) {
223
+ $cnt = 0;
224
+ $rs = 0;
225
+ for ($i = 0; $i < $k; $i++) {
226
+ if ($this->isVowel($s[$i])) {
227
+ $cnt++;
228
+ }
229
+ }
230
+ $rs = $cnt;
231
+ for ($j = $k; $j < strlen($s); $j++) {
232
+ if ($this->isVowel($s[$j - $k])) {
233
+ $cnt--;
234
+ }
235
+ if ($this->isVowel($s[$j])) {
236
+ $cnt++;
237
+ }
238
+ $rs = max($rs, $cnt);
239
+ }
240
+ return $rs;
241
+ }
242
+ }
243
+ ```
244
+
206
245
### ** ...**
207
246
208
247
```
Original file line number Diff line number Diff line change @@ -175,6 +175,43 @@ function maxVowels(s: string, k: number): number {
175
175
}
176
176
```
177
177
178
+ ``` php
179
+ class Solution {
180
+ /**
181
+ * @param String $s
182
+ * @param Integer $k
183
+ * @return Integer
184
+ */
185
+ function isVowel($c) {
186
+ return $c === 'a' ||
187
+ $c === 'e' ||
188
+ $c === 'i' ||
189
+ $c === 'o' ||
190
+ $c === 'u';
191
+ }
192
+ function maxVowels($s, $k) {
193
+ $cnt = 0;
194
+ $rs = 0;
195
+ for ($i = 0; $i < $k; $i++) {
196
+ if ($this->isVowel($s[$i])) {
197
+ $cnt++;
198
+ }
199
+ }
200
+ $rs = $cnt;
201
+ for ($j = $k; $j < strlen($s); $j++) {
202
+ if ($this->isVowel($s[$j - $k])) {
203
+ $cnt--;
204
+ }
205
+ if ($this->isVowel($s[$j])) {
206
+ $cnt++;
207
+ }
208
+ $rs = max($rs, $cnt);
209
+ }
210
+ return $rs;
211
+ }
212
+ }
213
+ ```
214
+
178
215
### ** ...**
179
216
180
217
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /**
3
+ * @param String $s
4
+ * @param Integer $k
5
+ * @return Integer
6
+ */
7
+ function isVowel ($c ) {
8
+ return $c === ' a' ||
9
+ $c === ' e' ||
10
+ $c === ' i' ||
11
+ $c === ' o' ||
12
+ $c === ' u' ;
13
+ }
14
+ function maxVowels ($s , $k ) {
15
+ $cnt = 0 ;
16
+ $rs = 0 ;
17
+ for ($i = 0 ; $i < $k ; $i ++ ) {
18
+ if ($this -> isVowel($s [$i ])) {
19
+ $cnt ++ ;
20
+ }
21
+ }
22
+ $rs = $cnt ;
23
+ for ($j = $k ; $j < strlen ($s ); $j ++ ) {
24
+ if ($this -> isVowel($s [$j - $k ])) {
25
+ $cnt -- ;
26
+ }
27
+ if ($this -> isVowel($s [$j ])) {
28
+ $cnt ++ ;
29
+ }
30
+ $rs = max ($rs , $cnt );
31
+ }
32
+ return $rs ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments