Tutorialspoint
Problem
Solution
Submissions

Word Search II

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Given an m x n board of characters and a list of strings words, find all words in 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.

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: ["eat","oath"]
  • Explanation:
    • Step 1: Consider the board and list of words.
    • Step 2: For "oath", we can find it by starting at board[0][0] and following the path: 'o' → 'a' → 't' → 'h'.
    • Step 3: For "eat", we can find it by starting at board[0][3] and following the path: 'e' → 'a' → 't'.
    • Step 4: For "pea" and "rain", we cannot find paths on the board.
    • Step 5: Return ["eat","oath"] as the result.
Example 2
  • Input: board = [
     ['a','b'],
     ['c','d'] ]
    words = ["abcb"]
  • Output: []
  • Explanation:
    • Step 1: Consider the board and list of words.
    • Step 2: For "abcb", we can find 'a' → 'b' → 'c', but then we need another 'b'.
    • Step 3: Since the same letter cell 'b' cannot be used twice, we cannot find "abcb" on the board.
    • Step 4: Return an empty list as the result.
Constraints
  • m == board.length
  • n == board[i].length
  • 1 ≤ m, n ≤ 12
  • board[i][j] is a lowercase English letter.
  • 1 ≤ words.length ≤ 3 * 10^4
  • 1 ≤ words[i].length ≤ 10
  • words[i] consists of lowercase English letters.
  • All the strings of words are unique.
  • Time Complexity: O(M * N * 4^L) where M,N are dimensions of board and L is max word length
  • Space Complexity: O(sum of word lengths)
Trie Dictionaries SnowflakePhillips
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a Trie data structure to store and search for words efficiently.
  • Implement a DFS traversal of the board to find words.
  • Mark visited cells during traversal to prevent reusing them.
  • Optimize by pruning paths that cannot form any word in the dictionary.
  • Keep track of found words to avoid duplicates.

Steps to solve by this approach:

 Step 1: Build a Trie data structure to efficiently store and search for words.

 Step 2: Insert all words from the dictionary into the Trie.
 Step 3: For each cell in the board, start a DFS traversal.
 Step 4: During traversal, check if the current path forms a prefix of any word in the Trie.
 Step 5: If a complete word is found, add it to the result set.
 Step 6: Mark cells as visited during traversal to avoid using the same cell multiple times.
 Step 7: Restore cells after exploring all paths from them. Step 8: Convert the result set to a list and return it.

Submitted Code :