Data Structures & AlgorithmsVery hard

In a sliding window tracking the maximum number of vowels among windows of fixed length 3, why is O(1) update per step possible?

  1. A. Vowels can be counted by sorting the window each time.
  2. B. The window must be rebuilt from scratch each step.
  3. C. Moving the window by one changes only one entering and one leaving character, so the vowel count changes by at most one.
  4. D. Vowel counts are always equal across windows.

Correct answer: C. Moving the window by one changes only one entering and one leaving character, so the vowel count changes by at most one.

Only two characters change between consecutive windows, so the count is updated with two checks. Rebuilding the count from scratch would cost the full window length each time.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic