text and pattern. Find the shortest substring of text (a contiguous piece) that contains pattern as a subsequence, meaning all letters of pattern appear in it in the same order, though not necessarily next to each other.
If several substrings share the minimum length, return the one that starts furthest left. If no substring fits, return the empty string "".text='abxbcab', pattern='bc' → 'bc'
The window "bxbc" also contains "bc" as a subsequence, but "bc" starting at position 3 is shorter.
text='adbaeb', pattern='ab' → 'adb'
Both "adb" and "aeb" fit and have length 3; take the one that starts further left.
text='abc', pattern='ca' → ''
No a comes after the c in the text, so no window fits.
text.length ≤ 2 · 10⁴pattern.length ≤ 100text and pattern consist of lowercase English letterstext='abxbcab', pattern='bc'
'bc'
Run checks the first 3 tests in your browser. Submit checks all 161 tests on the server, hidden ones included.