Shortest Substring Containing All Pattern Characters with Repeats
Company: Lyft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Given a string and a pattern, return the shortest contiguous substring that contains every pattern character with its full multiplicity, choosing the leftmost one on ties and an empty string when none exists. It tests efficient string scanning, character counting with duplicates, careful edge cases and a clear complexity analysis.
Constraints
- 1 <= len(s) <= 10^5
- 1 <= len(t) <= 10^5
- s and t consist of uppercase and lowercase English letters.
Examples
Input: ('AXXBXCXXAB', 'ABC')
Expected Output: 'CXXAB'
Explanation: Source Example 1: the only length-5 window holding A, B and C is indices 5-9, which ends at the last index; the other covering windows have length 6 or more.
Input: ('AABXBAA', 'AAB')
Expected Output: 'AAB'
Explanation: Source Example 2: 'AAB' (index 0) and 'BAA' (index 4) tie at length 3; the smaller start index wins.
Hints
- A substring qualifies exactly when, for every character, it contains at least as many copies as t does. Repeated characters in t must be counted, not just checked for presence.
- If a substring qualifies, every longer substring that contains it also qualifies; and if t is longer than s, nothing can qualify.
- 'a' and 'A' are different characters, and when two qualifying substrings have the same length, the one that starts at the smaller index wins.