MediumStringSliding Window

Maximum Number of Vowels in a Substring of Given Length

LeetCode
1 approach, code in all languages

You are given a string s made of lowercase English letters and an integer k. A vowel is any of the characters 'a', 'e', 'i', 'o', or 'u'.

Consider every contiguous substring of s whose length is exactly k. Your task is to return the greatest number of vowels that appear in any single one of those substrings.

If no substring of length k exists, there is nothing to evaluate, but the constraints guarantee that k never exceeds the length of s.

Example 1

Input: s = "abciiidef", k = 3

Output: 3

The substring "iii" spans three positions and every character is a vowel, giving the maximum count of 3.

Example 2

Input: s = "aeiou", k = 2

Output: 2

Every window of length 2 is composed entirely of vowels, so the best any window can reach is 2.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters.
  • 1 <= k <= s.length
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now