Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find the maximum vowels in a substring of size K

Find the maximum number of vowels contained in any substring of length K using a sliding window.

Python practice6 minStrings – Sliding Window & FrequencyBeginnerLast updated March 25, 2026

Problem statement

Given a string s and an integer k, return the maximum number of vowels in any substring of s with length k. Vowels are 'a', 'e', 'i', 'o', 'u' (lowercase). If k is 0 return 0. If k is greater than the length of s, return 0.

Task

Implement an efficient sliding-window algorithm to count vowels in fixed-size substrings and return the maximum count.

Examples

Basic example

Input

s = "abciiidef", k = 3

Output

3

The substring "iii" contains 3 vowels which is the maximum for any window of size 3.

Input format

Function max_vowels(s: str, k: int) -> int

Output format

Return an integer representing the maximum vowel count in any substring of length k.

Constraints

0 <= k <= len(s) <= 10^5. Expected O(n) time and O(1) extra space.

Samples

Sample 1

Input

s = "aeiou", k = 2

Output

2

Every window of size 2 contains 2 vowels at most.