Top 150 InterviewEasy

Word Search II

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

Problem Statement

Easy

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].

Constraints
  • 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

Examples

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

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.