150强访谈简单

最小窗口子串

“最小窗口子串”问题的详细指南和 Python 实现。

问题陈述

简单

给定长度分别为 mn 的两个字符串 st,返回 s 的最小窗口子字符串,使得 t 中的每个字符(包括重复字符)都包含在窗口中。如果不存在这样的子字符串,则返回空字符串 ""

答案保证是唯一的。

编写一个函数 minWindow(s: str, t: str) -> str

约束条件
  • m == len(s), n == len(t)
  • 1 <= m, n <= 10^5
  • s and t consist of uppercase and lowercase English letters

示例

Example 1
Input
s = "ADOBECODEBANC", t = "ABC"
Output
"BANC"
Explanation

The minimum window substring "BANC" (indices 9-12) contains 'A', 'B', and 'C' from 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. Since s only has one 'a', return empty string.

Need a Hint?
考虑使用滑动窗口特定的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。