Minimum Window Substring
Detailed guide and Python implementation for the 'Minimum Window Substring' problem.
1. 学ぶ
The 'Minimum Window Substring' problem is a key challenge in the Sliding Window section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Minimum Window Substring.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Minimum Window Substring carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- スライディング ウィンドウ アプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準のスライディング ウィンドウ問題のプロパティが適用されます。 ---パイセップ--- セットやヒープなどのスライディング ウィンドウ固有のデータ構造の使用を検討してください。 ---パイセップ--- スライディング ウィンドウの最大値 ---パイセップ--- 「スライディング ウィンドウの最大値」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 配列の左端から右端に移動するスライディング ウィンドウのサイズを表す整数 __PYCODE_0__ と整数 __PYCODE_1__ の配列が与えられます。ウィンドウには __PYCODE_2__ 番号のみが表示されます。毎回、スライディング ウィンドウは 1 位置ずつ右に移動します。 各スライディング ウィンドウの最大値を返します。 関数 __PYCODE_3__ を作成します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- 引き違い窓 ---パイセップ--- 「スライディング ウィンドウの最大値」問題は、スライディング ウィンドウのセクションにおける重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ のハードレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- スライディング ウィンドウの最大値のロジック フローを視覚化します。 ---パイセップ--- スライディング ウィンドウの最大値の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- スライディング ウィンドウ アプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準のスライディング ウィンドウ問題のプロパティが適用されます。 ---パイセップ--- セットやヒープなどのスライディング ウィンドウ固有のデータ構造の使用を検討してください。 ---パイセップ--- 有効な括弧 ---パイセップ--- 「有効な括弧」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 文字 __PYCODE_1__、__PYCODE_2__、__PYCODE_3__、__PYCODE_4__、__PYCODE_5__、および __PYCODE_6__ だけを含む文字列 __PYCODE_0__ が指定された場合、入力文字列が有効かどうかを判断します。 入力文字列は次の場合に有効です。 1. 開いた括弧は同じ種類の括弧で閉じなければなりません。 2. 開き括弧は正しい順序で閉じなければなりません。 3. すべての閉じ括弧には、同じタイプの対応する開き括弧があります。 関数 __PYCODE_7__ を作成します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- スタック ---パイセップ--- 「有効な括弧」の問題は、スタックセクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。
問題提起
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 answer is guaranteed to be unique.
Write a function 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
例
s = "ADOBECODEBANC", t = "ABC"
"BANC"
The minimum window substring "BANC" (indices 9-12) contains 'A', 'B', and 'C' from t.
s = "a", t = "a"
"a"
The entire string s is the minimum window.
s = "a", t = "aa"
""
Both 'a's from t must be included. Since s only has one 'a', return empty string.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
解決する準備はできましたか?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
インタビューの洞察とバリエーション
複雑さの分析の内訳
なぜ時間がかかるのか: Directly evaluates all possibilities.
なぜ宇宙なのか: Uses standard local memory.
なぜ時間がかかるのか: Optimized paths reduce total operations.
なぜ宇宙なのか: May trade memory for speed.
最適化されたソリューションの Python コード
最適化されたソリューションの Python コード
def min_window_opt(s, t):
if t == "": return ""
countT, window = {}, {}
for c in t: countT[c] = 1 + countT.get(c, 0)
have, need = 0, len(countT)
res, resLen = [-1, -1], float("infinity")
l = 0
for r in range(len(s)):
c = s[r]
window[c] = 1 + window.get(c, 0)
if c in countT and window[c] == countT[c]:
have += 1
while have == need:
if (r - l + 1) < resLen:
resLen = r - l + 1
res = [l, r]
window[s[l]] -= 1
if s[l] in countT and window[s[l]] < countT[s[l]]:
have -= 1
l += 1
l, r = res
return s[l : r + 1] if resLen != float("infinity") else ""ブルート フォース コード (スポイラーガード付き)
ブルート フォース コード (スポイラーガード付き)
def min_window_brute(s, t):
if not t: return ""
def contains_all(counts_s, counts_t):
for char in counts_t:
if counts_s.get(char, 0) < counts_t[char]:
return False
return True
t_counts = {}
for c in t: t_counts[c] = 1 + t_counts.get(c, 0)
res, res_len = "", float("inf")
for i in range(len(s)):
for j in range(i, len(s)):
sub = s[i : j + 1]
sub_counts = {}
for c in sub: sub_counts[c] = 1 + sub_counts.get(c, 0)
if contains_all(sub_counts, t_counts):
if (j - i + 1) < res_len:
res_len = j - i + 1
res = sub
return resAlgorithm Pattern Checklist
When dealing with Sliding Window data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Standard Sliding Window problem properties apply.
関連する質問
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推奨される Python リソース
関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。
Python ループ
Python ループを使用してデータを反復処理する方法を学びます。インタラクティブな例を使用して、for ループ、while ループ、ブレーク、継続、ループのベスト プラクティスをマスターします。
Python でリストを並べ替える方法
sort() メソッドとsorted() 関数を使用して、Python でリストを並べ替える方法を学びます。カスタムキーの並べ替えと逆順の例をご覧ください。
Python 文字列メソッドのチートシート
Python 文字列操作の完全なリファレンス ガイド。文字列プロパティの書式設定、検索、分割、置換、チェックをマスターします。
Python と JavaScript: どちらのプログラミング言語が最適ですか?
Python と JavaScript の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。