Talentd
FleetCode

1456. Maximum Number of Vowels in a Substring of Given Length

Medium63.0% acceptance6 companies

Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

Example 1:

Input: s = "abciiidef", k = 3
Output: 3
Explanation: The substring "iii" contains 3 vowel letters.

Example 2:

Input: s = "aeiou", k = 2
Output: 2
Explanation: Any substring of length 2 contains 2 vowels.

Example 3:

Input: s = "leetcode", k = 3
Output: 2
Explanation: "lee", "eet" and "ode" contain 2 vowels.

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.
  • 1 <= k <= s.length

Hints open one at a time

Hint 1
Keep a window of size k and maintain the number of vowels in it.
Hint 2
Keep moving the window and update the number of vowels while moving. Answer is max number of vowels of any window.
7:58
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxVowels(self, s: str, k: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"abciiidef"
3
Input
"aeiou"
2
Input
"leetcode"
3
Your code is saved on this device.Solution