Problem 531
Minimum Window Subsequence
Hard · 30 pts · String, Dynamic Programming, Two Pointers
Problem
Given strings S and T, find the shortest substring W of S such that T is a subsequence of W. If several shortest windows exist, print the one that starts furthest to the left. If there is no such window, print -1. One approach: for each position in S, keep for every prefix of T the latest start index from which that prefix can be matched ending here; this takes O(|S| × |T|) time.
Input
The first line contains S. The second line contains T. Both are lowercase letters.
Output
Print the shortest window, or -1.
Constraints
1 <= |S| <= 20000; 1 <= |T| <= 100
Example input
abcdebdde bde
Example output
bcde
Explanation
For S = abcdebdde and T = bde, both bcde and bdde contain bde as a subsequence and have length 4. bcde starts earlier, so the answer is bcde.