Proofs: Sliding window for minimum window substring
These are the human proofs of Sliding window for minimum window substring, at full length.
The sliding window returns the leftmost shortest window that contains the pattern full
This proof shows: correctness, termination.
We use the pseudocode of the page, with . For a substring and a character , write for the number of copies of in . A window with is valid if for every . For a start , let be the smallest with valid, or . Write with for the empty window, which is not valid because .
Lemma 1 (closed under super-windows). If is valid, and , then is valid. Equivalently, every part of a window that is not valid is not valid.
Proof. Each count in is at least the same count in .
Lemma 2 (the counter). Let for a window , and . Then is valid if and only if . Adding one copy of to decreases by 1 and decreases by 1 if and only if before the change. Removing one copy of increases by 1 and increases by 1 if and only if after the change.
Proof. if and only if every , which is validity. The term changes by 1 exactly when moves between 1 and 0, in either direction, and does not change otherwise.
Claim. At the start of each step of the loop of line L2, and after the loop with :
(W) ; and are the values of Lemma 2 for the window ; this window is not valid; for each start , , and the window was examined at line L6; and is the length and start of the shortest examined window, with the smallest start among the shortest, or if none was examined.
On return, the output is the leftmost shortest valid window, or the empty string if no window is valid.
Proof. Initialization. Line L1 sets the values of Lemma 2 for the empty window , which is not valid, and , so there is no start .
Maintenance. Assume (W) at the start of step . Lines L3 and L4 add to the window, and by Lemma 2 they keep and correct for . The loop of line L5 runs while is valid, by Lemma 2. Let be the value of before the loop. In each run with left end , the window is a part of , which is not valid by (W), so is not valid by Lemma 1, while is valid. So , and line L6 examines the window . Lines L7 to L9 remove and keep and correct for by Lemma 2. So after each run, every start below the new has and its window was examined. The loop ends when is not valid; it ends because is empty and not valid, so . Line L6 keeps the shortest examined window, and replaces it only by a strictly shorter one; since the windows are examined in increasing order of start, the kept one has the smallest start among the shortest. So (W) holds at the start of step .
Result. After the last step, (W) holds with . Every start has had its window examined. For every start , the window is a part of , which is not valid, so . Every valid window has , so it is at least as long as , which was examined. So the shortest examined window is a shortest valid window, and its start is the smallest among them: if two valid windows have the minimum length and starts , both are of the form , and the one with start was examined first. If no window is valid, nothing was examined, , and line L10 returns the empty string.
No skipped answer. The left end passes a start only after line L6 has examined , the shortest valid window with start . The right end passes an end only when is not valid, and by Lemma 1 no valid window with exists; the windows with are longer than or equal to it, and that window was examined. So no candidate for the minimum is lost.
Termination and cost. Each run of the loop of line L5 increases by 1, and always, so the loop of line L5 runs at most times in total. The loop of line L2 runs times. Each run costs . Line L1 costs . The total is .