Smallest Window Holding Every Letter
You are given a text and a word. Find the shortest stretch of the text, letters in a row, that contains every letter of the word, each as many times as the word has it, in any order. Other letters may be in the stretch too.
Return that stretch. If two are equally short, return the one that starts first. If no stretch contains every letter, return an empty string "". Upper and lower case are different letters.
Examples
text = "ADOBECODEBANC", word = "ABC" → "BANC"
text = "a", word = "a" → "a"
text = "a", word = "aa" → ""
text = "xaaybxa", word = "aab" → "aayb"In the first example "BANC" holds an A, a B and a C in four letters. In the last, the word needs two as and a b: "aayb" has them in four letters, and "aybxa" has them too, but in five.
Constraints
- The text holds between 0 and 20,000 letters, the word between 1 and 20,000.
- Aim for O(n + m): each letter of the text enters and leaves the window once.
Console
›Run to see what your code prints
Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.
Expected is the reference solution's answer, from the judge. An input that breaks the statement's other rules may get any answer.