Take this array:
[1, 2, 6, 4, 4, 1, 0, 0, 2, 0, 4, 1, 4, 2, 4, 8, 1, 2, 4]
There are quite a few places where [1, ..., 2, ..., 4]
appears, where ...
is any number of items. These include [1, 2, 6, 4]
, [1, 0, 0, 2, 0, 4]
, [1, 0, 0, 2, 0, 4, 1, 4]
, and [1, 2, 4]
. The shortest of these is simply [1, 2, 4]
.
Task:
Given two arrays of any (reasonable) data type you choose, find the shortest slice of the first which contains all items in the second in order. There is guaranteed to be at least one such slice.
If there are multiple, you may return any one of them. Neither array will be empty. Duplicate items may appear in the second list.
Test cases:
[1, 2] [1, 2] -> [1, 2]
[2, 1, 3, 2] [1, 2] -> [1, 3, 2]
[1, 2, 4, 8] [1, 4, 8] -> [1, 2, 4, 8]
[0, 1, 1, 0, 0, 1, 1, 1, 0] [0, 1, 0] -> [0, 1, 1, 0]
[1, 5, 3, 7, 1, 3, 3, 5, 7] [1, 3, 5, 7] -> [1, 3, 3, 5, 7]
[1, 2, 5, 1, 3, 5] [1, 5] -> [1, 2, 5] OR [1, 3, 5]
Other:
This is code-golf, so shortest answer in bytes per language wins!
[0,9]
? \$\endgroup\$