Dsa
Sliding Window
Hard
Relay Coverage Gap
You are deploying relay stations along a line. Some points have specific signal types. You are given a target set of signal types (as a string of characters) that must all be present in your window. Additionally, to ensure connectivity, the distance (difference in indices) between any two consecutive signal-carrying points in your window must not exceed G. Find the length of the shortest window that satisfies both conditions.
Input Format:
- Line 1: Integer G (max gap).
- Line 2: A string of unique characters representing the target signal types.
- Line 3: A string representing the available signals at each index ('.' means no signal).
Output Format:
- A single integer representing the minimum length, or 0 if impossible.
Example:
Input:
2
AB
A..B.A
Output:
3
(Explanation: Window 'B.A' at indices 3-5 contains A and B. Signal indices are 3 and 5. Gap is 2, which is <= G. Length 3.)
Key concepts
sliding_windowgreedy
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now