hardSliding WindowPattern: Sliding Window / Hash Map

Minimum Alarm Window Solution

Problem Statement

Given a string of event codes `events` and an array of alarm codes `alarms`, find the shortest continuous segment of `events` that contains all alarm codes in `alarms`. Return the length of the segment if found, otherwise return -1.

Examples

Example 1:
Input:undefined
Output:ace
Explanation: The shortest window is 'ace' which has all the alarms
Example 2:
Input:undefined
Output:bdf
Explanation: The shortest window is 'bdf' which has all the alarms
Example 3:
Input:undefined
Output:NO
Explanation: Alarm 'f' is not present in events

Constraints

  • 1 <= s.length, t.length <= 10^5
Time: O(N) Space: O(1)
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).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.