TikTok VO 面试真题解析:Minimum Window Substring 最小覆盖子串

15次阅读
没有评论

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

The testcases will be generated such that the answer is unique.

Example 1:

Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.

Example 2:

Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.

Example 3:

Input: s = "a", t = "aa"
Output: ""Explanation: Both'a's from t must be included in the window. Since the largest window of s only has one'a', return empty string.

这道题是经典的最小覆盖子串问题,核心是用滑动窗口在字符串 s 上动态扩展和收缩,维护窗口内每个字符的出现次数,并判断是否已经覆盖了 t 中的所有字符(包括重复字符)。当窗口满足条件时,尽量从左侧收缩以更新更短答案;当窗口不再满足时,再继续右扩张。配合哈希表统计频次,可以在 O(m+n) 级别的时间内完成,适合面试中考察双指针、字符计数和窗口维护能力。

正文完
 0