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?
- A. Z-function on the whole string, because it finds all windows directly
- B. Sliding window with a frequency map of the pattern and a count of satisfied letters
- C. Dynamic programming over all substring pairs, because windows overlap
- 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.
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
- Given an array of 10^6 values and window size k, you need the maximum over all windows of the window minimum. What is the most suitable…
- How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] contain at most two distinct values?
- You may increase any element of [2, 5, 3, 9] by 1 per operation, and you want all elements equal. What is the minimum number of operations?
- In the binary row [1, 1, 0, 1, 1, 1] treated as a histogram of height 1, what is the largest rectangle area?
- What is the length of the shortest contiguous subarray of [5, 1, 4, 3, 2, 6, 7] whose sum is at least 15?
- In [6, 3, 6, 9, 3, 9, 9, 5, 5] exactly one value appears an odd number of times and every other value appears an even number of times…