Minimum Window Subsequence

problem [https://leetcode.com/problems/minimum-window-subsequence/ 727. Minimum Window Subsequence].

Problem description
Given strings  and , return the minimum contiguous substring part of  , so that   is a subsequence of the part.

If there is no such window in  that covers all characters in , return the empty string. If there are multiple such minimum-length windows, return the one with the left-most starting index.


 * Example 1
 * Input:
 * Output:
 * Explanation:
 * is the answer because it occurs before  which has the same length.
 * is not a smaller window because the elements of  in the window must occur in order.


 * Example 2
 * Input:
 * Output:


 * Constraints
 * and  consist of lowercase English letters.
 * and  consist of lowercase English letters.
 * and  consist of lowercase English letters.

Analysis

 * Hint
 * Let  be the largest index for which   has   as a subsequence.
 * Here the start index is inclusive, and the end index is exclusive.

Let  and   be the length of   and   respectively. So we can see the ranges of j, e and s:
 * 0 &le; j &le;
 * 0 &le; e <
 * 0 &le; s &le; e + 1 &le;

Then we can create the  table with   rows and   columns. Each row corresponds to a j, and each column corresponds to an e. The table is filled up as follows:
 * Initialize all cells with -1 (assuming that no match was found).
 * Fill row 0 with values 1, 2, …, . This is because ,  ,  , … are all empty strings, which automatically contain the substring   in.
 * For : This cell will contain 0 if   and   are the same character, or -1 otherwise.
 * For all other cells:
 * Starting from row 2, the first  columns are untouched (filled with -1), meaning that the substring   is too short to contain.
 * Transition equations are as follows:
 * , if  (one new matching is found);
 * , otherwise.

Finally there is a post-processing step. We will traverse through row  of the   table, and find the substring window with minimum length and minimum starting position.
 * means that there is no substring in  ending at position e that will contain the entire   as a subsequence.
 * means that  is the maximum starting index such that   contains   as a subsequence. In this case the length is  . We can traverse the bottom row from left to right to find out which length is the smallest, and for the smallest length which index is the earliest possible index for the result.

Working example

 * Input
 * (9 characters),  (3 characters)


 * Filling up the  table
 * {| class="wikitable" style="text-align: center;"

! rowspan="3" colspan="3" | ! colspan="9" | ! 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 !  ||   ||   ||   ||   ||   ||   ||   ||  ! rowspan="4" |   || 0 || ! 1 || ! 2 ||  ! 3 ||
 * 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9
 * style="background-color: lime;" | 0 || 0 || style="background-color: lime;" | 2 || 2 || 2 || 2 || 2 || 2 || 2
 * -1 || style="background-color: lime;" | 0 || 0 || 0 || 0 || style="background-color: lime;" | 2 || 2 || 2 || 2
 * -1 || -1 || -1 || -1 || style="background-color: lime;" | 0 || 0 || 0 || 0 || style="background-color: lime;" | 2
 * }
 * Lime cells : matching positions


 * Post-processing
 * Initialize:,.
 * {| class="wikitable" style="text-align: center;"

! After iterating index || 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 ! !
 * + Iterating over row 3 of the  table
 * ||  ||   ||   || 5 || 5 || 5 || 5 || 5
 * -1 || -1 || -1 || -1 || 0 || 0 || 0 || 0 || 0
 * }


 * Output
 * , i.e..

Complexity analysis

 * Space complexity
 * Let  and   be the length of   and   respectively. The   table has (  + 1) ×   total cells. So the total space complexity is O(  ×  ).


 * Time complexity
 * To fill in the cells in the  table, each cell takes O(1) computation. So total time complexity is O(  ×  ).