Top 150 Interview簡単

Word Search II

Detailed guide and Python implementation for the 'Word Search II' problem.

問題提起

簡単

Given an m x n board of characters and a list of strings words, return all words on the board.

Each word must 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 in a word.

Write a function findWords(board: List[List[str]], words: List[str]) -> List[str].

制約
  • m == len(board)
  • n == len(board[i])
  • 1 <= m, n <= 12
  • board[i][j] is a lowercase English letter
  • 1 <= len(words) <= 3 * 10^4
  • 1 <= len(words[i]) <= 10
  • words consist of lowercase English letters
  • All the strings in words are unique

Example 1
Input
board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output
["oath","eat"]
Explanation

The words "oath" and "eat" can be found on the board. "pea" and "rain" cannot.

Example 2
Input
board = [["a","b"],["c","d"]], words = ["abcb"]
Output
[]
Explanation

The word "abcb" requires using the 'b' cell twice, which is invalid.

Need a Hint?
Consider using Trie-specific data structures like sets or heaps.
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.

エディタで開く
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 リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。