Amazon Online Assessment: Shortest Subarray Covering a Set of Strings

17 Views
No Comments

Write a program which takes an array of strings and a set of strings, and returns the starting and ending indices of a shortest subarray of the given array that covers the set, i.e., contains all strings in the set.

Example:

<apple, banana, apple, apple, dog, cat, apple, dog, banana, apple, cat, dog>
Set: <banana, cat>

Smallest subarray starts at 8, ends at 10.

This problem asks for the shortest contiguous subarray that contains every string in a target set. The standard solution uses a sliding window with a hash map or counter: expand the right pointer to collect required strings, then shrink the left pointer while the window still covers the set, updating the best interval along the way. In the example, the window must contain both <code>banana</code> and <code>cat</code>, and the minimum interval is <code>[8, 10]</code>.

END
 0