Prefix Sum
Medium

Count Vowels in Ranges

Efficiently answer multiple queries about the number of vowels in a range.
Problem Understanding

You are given a string array words and a 2D array queries. Each query [l, r] asks for the number of strings in words[l...r] that start and end with a vowel.

We need to answer many queries efficiently.

Strategy
  1. Preprocessing: Create a boolean/integer array is_valid where is_valid[i] = 1 if words[i] starts/ends with vowel, else 0.
  2. Prefix Sum: Compute prefix sum P of is_valid. P[i] = count of valid words in words[0...i-1].
  3. Query: Answer for [l, r] is P[r+1] - P[l].
  • Vowels: a, e, i, o, u

Interactive Visualization
Step 1 / 1

Initializing...

1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.
Unlock VisualizerPREMIUM FEATURE

Stop Guessing, Start Mastering.

Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.

Start Your Premium Prep