Given two arrays A[n] and B[m], how can I find the smallest window in A that contains all the elements of B.
I am trying to solve this problem in O(n) time but I am having problem doing it. Is there any well know algorithm or procedure for solving it.
countLet’s call window ‘minimal’ if it can’t be reduced. I.e., after increasing its left border or decreasing its right border it’s no longer valid window (doesn’t contain all elements from B). There three in your example: [0, 2], [2, 6], [6, 7]
Let’s assume say that you already found leftmost minimal window [left, right]. ([0, 2] in your example) Now we’ll just slide it to the right.
This sliding works because different ‘minimal’ windows can’t contain one another.