Prefix Sum pattern

Count Vowels in Substrings

Given a string, precompute prefix counts of vowels so that the number of vowels in any substring range can be answered in constant time per query. Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • 2 approaches

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

Prefix Sum

From the first idea to the best one

  1. 01 Brute force · scan each query O(n · q)O(1) space
  2. 02 Optimized · prefix vowel counts O(n + q)O(n) space
Each approach plays step by step, with its time and space.

What you will learn

  • How to read Count Vowels in Substrings and spot the prefix sum pattern in it
  • Brute force · scan each query: O(n · q) time, O(1) space
  • Optimized · prefix vowel counts: O(n + q) time, O(n) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Prefix Sum

See Count Vowels in Substrings run, step by step

Sign in and this page opens on the animation, with all 2 Prefix Sum problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.