diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md index 94b7d19927ee3..969566f28d1d4 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README.md @@ -203,6 +203,45 @@ function maxVowels(s: string, k: number): number { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @param Integer $k + * @return Integer + */ + function isVowel($c) { + return $c === 'a' || + $c === 'e' || + $c === 'i' || + $c === 'o' || + $c === 'u'; + } + function maxVowels($s, $k) { + $cnt = 0; + $rs = 0; + for ($i = 0; $i < $k; $i++) { + if ($this->isVowel($s[$i])) { + $cnt++; + } + } + $rs = $cnt; + for ($j = $k; $j < strlen($s); $j++) { + if ($this->isVowel($s[$j - $k])) { + $cnt--; + } + if ($this->isVowel($s[$j])) { + $cnt++; + } + $rs = max($rs, $cnt); + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md index 4b03b76e29610..7c162ad586429 100644 --- a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/README_EN.md @@ -175,6 +175,43 @@ function maxVowels(s: string, k: number): number { } ``` +```php +class Solution { + /** + * @param String $s + * @param Integer $k + * @return Integer + */ + function isVowel($c) { + return $c === 'a' || + $c === 'e' || + $c === 'i' || + $c === 'o' || + $c === 'u'; + } + function maxVowels($s, $k) { + $cnt = 0; + $rs = 0; + for ($i = 0; $i < $k; $i++) { + if ($this->isVowel($s[$i])) { + $cnt++; + } + } + $rs = $cnt; + for ($j = $k; $j < strlen($s); $j++) { + if ($this->isVowel($s[$j - $k])) { + $cnt--; + } + if ($this->isVowel($s[$j])) { + $cnt++; + } + $rs = max($rs, $cnt); + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.php b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.php new file mode 100644 index 0000000000000..67c6615305982 --- /dev/null +++ b/solution/1400-1499/1456.Maximum Number of Vowels in a Substring of Given Length/Solution.php @@ -0,0 +1,34 @@ +class Solution { + /** + * @param String $s + * @param Integer $k + * @return Integer + */ + function isVowel($c) { + return $c === 'a' || + $c === 'e' || + $c === 'i' || + $c === 'o' || + $c === 'u'; + } + function maxVowels($s, $k) { + $cnt = 0; + $rs = 0; + for ($i = 0; $i < $k; $i++) { + if ($this->isVowel($s[$i])) { + $cnt++; + } + } + $rs = $cnt; + for ($j = $k; $j < strlen($s); $j++) { + if ($this->isVowel($s[$j - $k])) { + $cnt--; + } + if ($this->isVowel($s[$j])) { + $cnt++; + } + $rs = max($rs, $cnt); + } + return $rs; + } +}