00:00

Problem Statement

Input/Output Examples

Visual Explanation

Each slide adds one character and removes one, so the vowel count changes by at most one in each direction. Updating it costs O(1) instead of recounting k characters.

Approaches

The sliding window updates one count. The prefix-sum version precomputes counts so that any window can be measured with a subtraction.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the length of the string. Both approaches are linear; the prefix-sum version also stores n + 1 integers.

Interview Tips and Recommended Approach

  • Recognise the fixed window. Every window has the same length, so only the two ends change.
  • Stop early if possible. If the count ever equals k, no window can do better.
  • Know prefix sums. They turn any range-count question into a subtraction.
  • Use a lookup for vowels. A boolean[26] or "aeiou".indexOf(c) keeps the check short.

Recommended Approach: Fixed Sliding Window

Why: it scans the string once with constant extra memory, and the add-one, remove-one update is the core of every fixed-size window problem.