Magicsheet logo

Maximum Number of Vowels in a Substring of Given Length

Medium
51.8%
Updated 6/1/2025

Maximum Number of Vowels in a Substring of Given Length

What is this problem about?

The Maximum Number of Vowels in a Substring of Given Length coding problem gives you a string s and an integer k. You need to find the maximum number of vowel letters ('a', 'e', 'i', 'o', 'u') that are contained in any substring of s of length k.

Why is this asked in interviews?

Microsoft, tcs, Wayfair, Amazon, Turing, Google, and Bloomberg use this problem as a straightforward application of the sliding window technique. It's a common warm-up problem to test basic string processing and window maintenance skills.

Algorithmic pattern used

Sliding Window: This problem is a perfect candidate for the fixed-size sliding window pattern.

  1. Initialize Window: Calculate the number of vowels in the first substring of length k. This will be your initial current_vowel_count and max_vowel_count.
  2. Slide Window: Iterate through the string from index k to len(s) - 1. For each step:
    • Remove leftmost character: If the character s[i-k] (the one exiting the window) is a vowel, decrement current_vowel_count.
    • Add rightmost character: If the character s[i] (the one entering the window) is a vowel, increment current_vowel_count.
    • Update maximum: Update max_vowel_count = max(max_vowel_count, current_vowel_count).
  3. Return max_vowel_count.

A helper function is_vowel(char) simplifies the vowel check.

Example explanation

s = "abciiidef", k = 3. Vowels: a, e, i, o, u.

  1. Initial window (first k=3 characters): "abc"

    • Vowels: 'a', 'i'. current_vowel_count = 1. max_vowel_count = 1.
  2. Slide window:

    • i = 3: window "bci" (remove 'a', add 'i')

      • Remove 'a': is_vowel('a') is true. current_vowel_count = 1 - 1 = 0.
      • Add 'i': is_vowel('i') is true. current_vowel_count = 0 + 1 = 1.
      • max_vowel_count = max(1, 1) = 1.
    • i = 4: window "cii" (remove 'b', add 'i')

      • Remove 'b': is_vowel('b') is false. current_vowel_count = 1.
      • Add 'i': is_vowel('i') is true. current_vowel_count = 1 + 1 = 2.
      • max_vowel_count = max(1, 2) = 2.
    • i = 5: window "iii" (remove 'c', add 'i')

      • Remove 'c': is_vowel('c') is false. current_vowel_count = 2.
      • Add 'i': is_vowel('i') is true. current_vowel_count = 2 + 1 = 3.
      • max_vowel_count = max(2, 3) = 3.
    • i = 6: window "iid" (remove 'i', add 'd')

      • Remove 'i': is_vowel('i') is true. current_vowel_count = 3 - 1 = 2.
      • Add 'd': is_vowel('d') is false. current_vowel_count = 2.
      • max_vowel_count = max(3, 2) = 3.
    • ...and so on.

The maximum number of vowels found is 3.

Common mistakes candidates make

  • Not using a fixed-size window: Attempting to use a variable-size window or brute-force check on all substrings, leading to O(N*k) or O(N^2) complexity.
  • Incorrect vowel check: Simple typo or missing a vowel.
  • Off-by-one errors in window movement: Incorrectly identifying the character to remove or add.

Interview preparation tip

For the Sliding Window String interview pattern, this problem is fundamental. Master the mechanism of maintaining counts or sums efficiently as the window slides. This pattern is very versatile and appears in many string and array problems.

Similar Questions