Data Structures & AlgorithmsVery hard

For an anagram search with a fixed window of length 3, why is a count comparison cheap per step?

  1. A. Anagrams can be found by sorting every window once.
  2. B. The pattern always has a unique letter.
  3. C. Sliding by one character changes only two letter counts, so the comparison is updated in O(1) time.
  4. D. The count array is recomputed from scratch each step, which is cheap.

Correct answer: C. Sliding by one character changes only two letter counts, so the comparison is updated in O(1) time.

Only the entering and leaving characters change their counts, so the window's count vector is maintained in constant time. Recomputing it from scratch would cost the full window length each step.

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

Exams that ask this topic