Skip to content

Commit 69f936c

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 1456
1 parent 7bfdf3a commit 69f936c

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@
263263
- [1143 Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/description/)
264264
- [1218 Longest Arithmetic Subsequence of Given Difference](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/description/)
265265
- [1312 Minimum Insertion Steps to Make a String Palindrome](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/description/)
266+
- [1456 Maximum Number of Vowels in a Substring of Given Length](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/)
266267
- [1534 Count Good Triplets](https://leetcode.com/problems/count-good-triplets/description/)
267268
- [1679 Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/description/)
268269
- [1732 Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/description/)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def maxVowels(self, s: str, k: int) -> int:
5+
"""
6+
Given a string s and an integer k, return the maximum number of vowel letters in
7+
any substring of s with length k.
8+
9+
Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
10+
"""
11+
# Count initial sliding window
12+
curr = 0
13+
for i in range(k):
14+
if s[i] in ["a", "e", "i", "o", "u"]:
15+
curr += 1
16+
result = curr
17+
18+
# Move sliding window
19+
i = 1
20+
j = k
21+
while j < len(s):
22+
if s[i - 1] in ["a", "e", "i", "o", "u"]:
23+
curr -= 1
24+
result = max(result, curr)
25+
if s[j] in ["a", "e", "i", "o", "u"]:
26+
curr += 1
27+
result = max(result, curr)
28+
i += 1
29+
j += 1
30+
return result
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._1456_maximum_number_of_vowels_in_a_substring_of_given_length import ( # noqa: E501
4+
Solution,
5+
)
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["s", "k", "expected"],
10+
argvalues=[
11+
("abciiidef", 3, 3),
12+
("aeiou", 2, 2),
13+
("leetcode", 3, 2),
14+
],
15+
)
16+
def test_func(s: str, k: int, expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
max_vowels = Solution().maxVowels(s, k)
19+
assert max_vowels == expected

0 commit comments

Comments
 (0)