Data Structures & AlgorithmsVery hard
For an anagram search with a fixed window of length 3, why is a count comparison cheap per step?
- A. Anagrams can be found by sorting every window once.
- B. The pattern always has a unique letter.
- C. Sliding by one character changes only two letter counts, so the comparison is updated in O(1) time.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For the 'ramp width' problem (max j − i with A[i] ≤ A[j]), why are only prefix minima and suffix maxima worth considering?
- Why does pairing the heaviest remaining person with the lightest one (when they fit together) minimise boats when each boat carries at…
- The minimum number of consecutive deletions to make an array non-decreasing is found by which structure?
- Why is k ≥ n a necessary condition for the residues of n values modulo k to be distinct?
- When counting subsequences 'ace' in a string, why does a DP over the pattern prefixes avoid double counting?
- To count subarrays with equal numbers of even and odd values, which transformation makes the problem a zero-sum subarray problem?