Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 58130bc

Browse files
authored
feat: add php solution to lc problem: No.1456 (#1083)
1 parent 59fedcb commit 58130bc

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

‎solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,45 @@ function maxVowels(s: string, k: number): number {
203203
}
204204
```
205205

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+
206245
### **...**
207246

208247
```

‎solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,43 @@ function maxVowels(s: string, k: number): number {
175175
}
176176
```
177177

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+
178215
### **...**
179216

180217
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /