The part that tripped me up wasn't the expand-right logic, it was the matched counter.
Use a sliding window (two pointers) to expand and contract a window over s while tracking character counts of t. Maintain a count of matched characters to know when the window is valid, and record the minimum length window that contains all characters of t.
Pro tip: Clarify edge cases upfront (e.g., t longer than s, empty strings) and mention that the algorithm runs in O(n + m) time, which is optimal. Also, discuss how you would handle Unicode characters if relevant.
Confirm that the substring must contain all characters of t including duplicates, and that order does not matter. Discuss edge cases: t longer than s, empty strings, or no valid substring.
Explain that a brute-force check of all substrings is O(n^2 * m), but a sliding window with character frequency maps achieves O(n + m). Use two pointers to represent the window boundaries.
Move the right pointer to include characters, updating a frequency map of the current window. Track how many characters from t are fully matched (i.e., window count >= required count).
Once all characters are matched, move the left pointer to shrink the window while it remains valid. Update the minimum length and starting index whenever a smaller valid window is found.
After the right pointer reaches the end, return the substring using the recorded start and min length, or empty string if none. State time complexity O(n + m) and space O(m) for the frequency maps.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.