Data Structures & AlgorithmsModerateGATE CS · TCS NQT

For a string of length 10^6 and a pattern of 20 letters, you need the shortest substring containing every pattern letter with multiplicity. Which technique is the natural fit?

  1. A. Z-function on the whole string, because it finds all windows directly
  2. B. Sliding window with a frequency map of the pattern and a count of satisfied letters
  3. C. Dynamic programming over all substring pairs, because windows overlap
  4. D. Floyd-Warshall on the letter graph, because letters are connected

Correct answer: B. Sliding window with a frequency map of the pattern and a count of satisfied letters

A sliding window keeps the left and right ends moving forward once, so the total work is linear. Substring DP over all pairs is quadratic and too slow at this size.

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

Exams that ask this topic