文字列内の置換 ---パイセップ--- 「文字列の置換」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 2 つの文字列 __PYCODE_0__ と __PYCODE_1__ を指定すると、__PYCODE_3__ に __PYCODE_4__ の順列が含まれる場合は __PYCODE_2__ を返し、そうでない場合は __PYCODE_5__ を返します。 つまり、__PYCODE_7__ の順列の 1 つが __PYCODE_8__ の部分文字列である場合は、__PYCODE_6__ を返します。 関数 __PYCODE_9__ を作成します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- 引き違い窓 ---パイセップ--- 「文字列の置換」問題は、スライディング ウィンドウ セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- Permutation In String のロジック フローを視覚化します。 ---パイセップ--- Permutation In String の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- スライディング ウィンドウ アプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準のスライディング ウィンドウ問題のプロパティが適用されます。 ---パイセップ--- セットやヒープなどのスライディング ウィンドウ固有のデータ構造の使用を検討してください。 ---パイセップ--- 最小ウィンドウ部分文字列 ---パイセップ--- 「最小ウィンドウ部分文字列」問題の詳細なガイドと __PYTERM_0__ 実装。 ---パイセップ--- それぞれ __PYCODE_2__ と __PYCODE_3__ の長さの 2 つの文字列 __PYCODE_0__ と __PYCODE_1__ がある場合、__PYCODE_5__ のすべての文字 (重複を含む) がウィンドウに含まれるように、最小のウィンドウ部分文字列 __PYCODE_4__ を返します。そのような部分文字列が存在しない場合は、空の文字列 __PYCODE_6__ を返します。 答えは一意であることが保証されています。 関数 __PYCODE_7__ を作成します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- 引き違い窓 ---パイセップ--- 「最小ウィンドウ部分文字列」問題は、スライディング ウィンドウ セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 最小ウィンドウ部分文字列のロジック フローを視覚化します。 ---パイセップ--- 最小ウィンドウ部分文字列の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。
Detailed guide and Python implementation for the 'Permutation In String' problem.
1. 学ぶ
The 'Permutation In String' 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 Permutation In String.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Permutation In String 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
Clean up the code for production standards.
問題提起
Given two strings s1 and s2, return True if s2 contains a permutation of s1, or False otherwise.
In other words, return True if one of s1's permutations is a substring of s2.
Write a function checkInclusion(s1: str, s2: str) -> bool.
- •1 <= len(s1), len(s2) <= 10^4
- •s1 and s2 consist of lowercase English letters
例
s1 = "ab", s2 = "eidbaooo"
True
s2 contains one permutation of s1: "ba" (starting at index 3).
s1 = "ab", s2 = "eidboaoo"
False
No permutation of "ab" exists as a contiguous substring in s2.
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 check_inclusion_opt(s1, s2):
if len(s1) > len(s2): return False
s1Count, s2Count = [0] * 26, [0] * 26
for i in range(len(s1)):
s1Count[ord(s1[i]) - ord('a')] += 1
s2Count[ord(s2[i]) - ord('a')] += 1
matches = 0
for i in range(26):
if s1Count[i] == s2Count[i]: matches += 1
l = 0
for r in range(len(s1), len(s2)):
if matches == 26: return True
index = ord(s2[r]) - ord('a')
s2Count[index] += 1
if s1Count[index] == s2Count[index]: matches += 1
elif s1Count[index] + 1 == s2Count[index]: matches -= 1
index = ord(s2[l]) - ord('a')
s2Count[index] -= 1
if s1Count[index] == s2Count[index]: matches += 1
elif s1Count[index] - 1 == s2Count[index]: matches -= 1
l += 1
return matches == 26ブルート フォース コード (スポイラーガード付き)
ブルート フォース コード (スポイラーガード付き)
def check_inclusion_brute(s1, s2):
n1, n2 = len(s1), len(s2)
if n1 > n2: return False
s1_sorted = sorted(s1)
for i in range(n2 - n1 + 1):
if sorted(s2[i : i + n1]) == s1_sorted:
return True
return FalseAlgorithm 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 Try/Except とエラー処理
Python スクリプトがクラッシュしないようにします。 try、excel、finally ブロックと、カスタム例外を適切に発生させる方法について学びます。
Python で文字列を反転する方法
スライス、reversed() 関数、ループ連結を使用して Python で文字列を反転する方法を、ビジュアル コード例とともに学びます。
Python 文字列メソッドのチートシート
Python 文字列操作の完全なリファレンス ガイド。文字列プロパティの書式設定、検索、分割、置換、チェックをマスターします。
Python と JavaScript: どちらのプログラミング言語が最適ですか?
Python と JavaScript の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。