Word Search
Detailed guide and Python implementation for the 'Word Search' problem.
1. 学ぶ
The 'Word Search' problem is a key challenge in the Backtracking 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 Word Search.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Word Search 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__ 実装。 ---パイセップ--- 文字列 s が与えられた場合、パーティションのすべての部分文字列が回文になるように s をパーティション化します。可能なすべての s の回文分割を返します。 関数 __PYCODE_0__ を実装します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- 後戻り ---パイセップ--- 「回文分割」問題は、バックトラッキングセクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 回文パーティショニングのロジック フローを視覚化します。 ---パイセップ--- 回文パーティショニングの問題ステートメントを注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- バックトラッキングアプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準的なバックトラッキング問題のプロパティが適用されます。 ---パイセップ--- セットやヒープなどのバックトラッキング固有のデータ構造の使用を検討してください。 ---パイセップ--- 電話番号の文字の組み合わせ ---パイセップ--- 「電話番号の文字の組み合わせ」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 2 ~ 9 の数字を含む文字列を指定すると、その数字が表すことができるすべての文字の組み合わせを返します。任意の順序で答えを返します。 (電話のボタンと同様に) 数字と文字のマッピングを以下に示します。 1 はどの文字にもマップされないことに注意してください。 2: abc、3: def、4: ghi、5: jkl、6: mno、7: pqrs、8: tuv、9: wxyz 関数 __PYCODE_0__ を実装します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- 後戻り ---パイセップ--- 「電話番号の文字の組み合わせ」問題は、バックトラッキング セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。
問題提起
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Implement a function exist(board: list, word: str) -> bool.
- •m == board.length
- •n == board[i].length
- •1 <= m, n <= 6
- •1 <= word.length <= 15
- •board and word consists of only lowercase and uppercase English letters
例
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED"
True
The word ABCCED can be traced: A(0,0)->B(0,1)->C(0,2)->C(1,2)->E(2,2)->D(2,1).
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE"
True
The word SEE can be traced: S(1,3)->E(2,3)->E(2,2).
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB"
False
Cannot trace ABCB without reusing cells.
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 exist_opt(board: list[list[str]], word: str) -> bool:
rows, cols = len(board), len(board[0])
path = set()
def dfs(r, c, i):
if i == len(word): return True
if r < 0 or c < 0 or r >= rows or c >= cols or board[r][c] != word[i] or (r, c) in path:
return False
path.add((r, c))
res = dfs(r + 1, c, i + 1) or dfs(r - 1, c, i + 1) or dfs(r, c + 1, i + 1) or dfs(r, c - 1, i + 1)
path.remove((r, c))
return res
from collections import Counter
count = Counter(word)
board_count = Counter(char for row in board for char in row)
for char in count:
if count[char] > board_count[char]: return False
if board_count[word[0]] > board_count[word[-1]]: word = word[::-1]
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0): return True
return Falseブルート フォース コード (スポイラーガード付き)
ブルート フォース コード (スポイラーガード付き)
def exist_brute(board: list[list[str]], word: str) -> bool:
rows, cols = len(board), len(board[0])
def dfs(r, c, i, visited):
if i == len(word): return True
if r < 0 or c < 0 or r >= rows or c >= cols or (r, c) in visited or board[r][c] != word[i]:
return False
visited.add((r, c))
res = dfs(r + 1, c, i + 1, visited) or dfs(r - 1, c, i + 1, visited) or dfs(r, c + 1, i + 1, visited) or dfs(r, c - 1, i + 1, visited)
visited.remove((r, c))
return res
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0, set()): return True
return FalseAlgorithm Pattern Checklist
When dealing with Backtracking data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Standard Backtracking 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 の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。