CodePark/ problems / Shortest Covering Substring
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Sliding window

Shortest Covering Substring

hardabout 35 minutes in an interview

Find the shortest substring of s that holds every character of t, counting repeats. A substring is a block of characters that sit side by side in s. So if t has two as, the block needs at least two as too.

  • If no block of s holds all of t, return "".
  • If several blocks tie for shortest, return the leftmost one, the one that starts first.
  • Upper and lower case are different characters. t is never empty.
  • s can be up to 100,000 characters long.
Input: s = "xaybzcab", t = "abc" Output: "cab"
Input: s = "abca", t = "aa" Output: "abca"Both a's are needed, so the substring must stretch from the first a to the last a.
Input: s = "abc", t = "d" Output: ""