Word Search II

Problem for Word Search II

You are given a 2D grid of letters (board) and a list of words. Your goal is to find all the words from the list that can be formed by following a continuous path on the grid.

A word is considered valid if its letters can be connected using adjacent cells in the grid, either horizontally or vertically. However, you cannot use the same cell more than once while forming a word. Return all the words from the list that meet these conditions.

Word Search II Problem

Example 1:

Example of Word Stream II

Example 2:

Example 2 of Word Search II

Constraints:

  • 1 <= board.length, board[i].length <= 10
  • board[i] consists only of lowercase English letter.
  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • words[i] consists only of lowercase English letters.
  • All strings within words are distinct.

Word Search II Solution

Recommendation for Time and Space Complexity – Target O(m * n * 4 * (3^(t-1)) + s) time and O(s) space, where:

  • m, n: Grid dimensions.
  • t: Longest word length.
  • s: Total word lengths.

Hints for solving problems

Hint 1 :

Use backtracking to search for a word by recursively matching characters from each grid cell. However, running backtracking separately for all words is inefficient. Can you optimize this with a better data structure?

Hint 2 :

Insert all words into a Trie. Traverse the grid once, starting from each cell. If a cell matches a Trie node, continue searching; otherwise, stop. When you reach an “end of word” in the Trie, you’ve found a valid word.

Hint 3 :

Store word indices in Trie nodes to quickly retrieve words during the search. Add the word to the result list when found, and mark its index as used to prevent duplicates.

Hint 4 :

Insert all words into the Trie, then explore the grid. Match characters recursively using the Trie. Add words to the result upon finding a match, and continue exploring other paths to find all possible words.

There are mainly 3 approach to solve this problem-

  1. Backtracking Method
  2. Backtracking(Trie + Hash Set) Method
  3. Backtracking(Trie) Method

1. Backtracking Method

Search each word by exploring all paths in the grid using backtracking. This method is simple but inefficient for large inputs due to repeated searches.

  • Time complexity: O(m * n * 4^t + s)
  • Space complexity: O(t)

where m is the number of rows, n is the number of columns, t is the maximum length of any word in the array words and s is the sum of the lengths of all the words.

Code

2. Backtracking(Trie + Hash Set) Method

Use a Trie to store words and a Hash Set to track found words. Backtracking checks characters in the Trie, adding valid words to the result set to improve efficiency.

  • Time complexity: <latex>O\left ( m\times n \times 4 \times 3^{t-1} + s\right )</latex>.
  • Space complexity: O(s)

where m is the number of rows, n is the number of columns, t is the maximum length of any word in the array words and s is the sum of the lengths of all the words.

Code

3. Backtracking(Trie) Method

Use a Trie to store words and a Hash Set to track found words. Backtracking checks characters in the Trie, adding valid words to the result set to improve efficiency.

  • Time complexity: O(m * n * 4t * 3^(t-1) + s).
  • Space complexity: O(s)

where m is the number of rows, n is the number of columns, t is the maximum length of any word in the array words and s is the sum of the lengths of all the words.

Code

More Articles