BackhardSliding WindowNetflix

Minimum Window Extractor Solution

Problem Statement

A surveillance system scans a video feed (represented as a string of event codes). Find the shortest continuous segment of feed that contains all the alarm codes specified in a given pattern. Return the shortest window containing all the alarm codes. Return 'NO' if not found.

Example 1
Input
video feed: ABCDE, pattern: A, B, C
Output
ABC

Explanation: Step-by-step: The pattern A, B, C is not found in the given video feed ABCDE. The solution should return the shortest window containing all the alarm codes. The shortest window is 'ABC'.

Example 2
Input
video feed: ABCDE, pattern: A, C, E
Output
NO

Explanation: Step-by-step: The pattern A, C, E is not found in the given video feed ABCDE. The solution should return 'NO' because there is no window that contains all the alarm codes.

Constraints

  • 1 <= s.length, t.length <= 10^5
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Minimum Window Extractor — Problem Statement & Solution Guide

Sliding WindowHardSliding Window / Hash Map
TimeO(n)
|
SpaceO(1)

Problem Description

A surveillance system scans a video feed (represented as a string of event codes). Find the shortest continuous segment of feed that contains all the alarm codes specified in a given pattern. Return the shortest window containing all the alarm codes. Return 'NO' if not found.

Examples

Example 1

Input

video feed: ABCDE, pattern: A, B, C

Output

ABC

Explanation: Step-by-step: The pattern A, B, C is not found in the given video feed ABCDE. The solution should return the shortest window containing all the alarm codes. The shortest window is 'ABC'.

Example 2

Input

video feed: ABCDE, pattern: A, C, E

Output

NO

Explanation: Step-by-step: The pattern A, C, E is not found in the given video feed ABCDE. The solution should return 'NO' because there is no window that contains all the alarm codes.

Constraints

  • 1 <= s.length, t.length <= 10^5

Optimal Approach & Strategy

Two HashMaps/arrays. One for required chars, one for current window. Expand right. When window has all chars of t, shrink left to minimize. Keep track of smallest window. Time O(N), Space O(1) (256 ASCII chars).

Brute Force Approach

Check every substring of s. Time O(N^3).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function minWindowExtractor(videoFeed, pattern) {
  if (!videoFeed || !pattern) return 'NO';
  let patternSet = new Set(pattern);
  let windowStart = 0;
  let minWindow = videoFeed.length + 1;
  let formed = 0;
  let windowCounts = {};

  for (let windowEnd = 0; windowEnd < videoFeed.length; windowEnd++) {
    let rightChar = videoFeed[windowEnd];
    windowCounts[rightChar] = (windowCounts[rightChar] || 0) + 1;

    if (patternSet.has(rightChar) && windowCounts[rightChar] === patternSet.get(rightChar)) {
      formed++;
    }

    while (formed === patternSet.size) {
      let leftChar = videoFeed[windowStart];
      if (windowEnd - windowStart + 1 < minWindow) {
        minWindow = windowEnd - windowStart + 1;
      }
      windowCounts[leftChar]--;
      if (patternSet.has(leftChar) && windowCounts[leftChar] < patternSet.get(leftChar)) {
        formed--;
      }
      windowStart++;
    }
  }

  return minWindow > videoFeed.length ? 'NO' : videoFeed.slice(minWindow - 1, minWindow);
}

Asked in Top Tech Interviews

Netflix

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.