Minimum Window Substring

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Write minWindow(text, need): return the shortest stretch of text, as a string, that contains every character of need, counting repeats, so a need of "aab" needs two a's and a b. Order doesn't matter, and letters are case-sensitive. need has at least one character. If several stretches are equally short, return the one that starts first; if there's none, return "". For example, minWindow("ADOBECODEBANC", "ABC") is "BANC". The checks include 100,000 characters, so growing a stretch from every start is too slow.

Your code must pass

  • Finds the shortest stretch containing every character
  • Counts repeated characters in need
  • Returns the first of equally short stretches
  • Returns an empty string when there's no such stretch
  • Is fast enough for 100,000 characters