Word Search II - Complete Solution Guide
Word Search II is LeetCode problem 212, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. 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"] Example 2: Input: board = [["a","b"],["c
Detailed Explanation
The Word Search II problem asks us to find all words from a given list that exist within a 2D board of characters. Words must be formed by connecting adjacent cells (horizontally or vertically), and each cell can only be used once in a word. The input consists of the board (a 2D array of characters) and a list of words (strings). The output is a list of all words from the given list that are found on the board. The constraints specify the dimensions of the board, the length of the words, and the characters involved (lowercase English letters).
Solution Approach
The provided solution uses a combination of Trie data structure and Depth-First Search (DFS) with backtracking. First, a Trie is built using the given list of words. Then, a DFS is performed starting from each cell of the board. During the DFS, the current character is checked against the Trie. If the character exists as a child of the current Trie node, the search continues recursively in all four directions (up, down, left, right). When a complete word is found, it's added to the result list, and the word is marked as visited in the Trie to prevent duplicates. The board's cells are temporarily marked as visited using a special character to prevent reusing the same cell within a word.
Step-by-Step Algorithm
- Step 1: Build a Trie data structure from the list of words. Each node in the Trie represents a character, and a path from the root to a node represents a prefix of a word. A flag is set at the end of each complete word.
- Step 2: Iterate through each cell of the board. For each cell, check if the character in that cell is a child of the root node of the Trie. If it is, initiate a DFS starting from that cell.
- Step 3: The DFS function takes the current row, column, and the current Trie node as input. It checks if the current Trie node represents the end of a word. If so, it adds the word to the result list and marks the word as visited (by setting the `word` field in the TrieNode to null).
- Step 4: Mark the current cell as visited by temporarily changing its value to a special character (e.g., '#'). This prevents the same cell from being used multiple times in a single word.
- Step 5: Recursively call the DFS function for all four adjacent cells (up, down, left, right), passing the corresponding child node of the current Trie node as the new current Trie node.
- Step 6: After exploring all adjacent cells, backtrack by restoring the original value of the current cell and removing empty Trie nodes when possible.
- Step 7: After iterating through all cells of the board, return the list of found words.
Key Insights
- Insight 1: Using Trie data structure is crucial for efficient prefix matching. It allows us to quickly check if a given path in the board can potentially lead to any of the target words.
- Insight 2: Backtracking is essential for exploring different paths on the board to search for possible words. It involves recursively exploring adjacent cells while keeping track of the current word being formed.
- Insight 3: Optimizations like pruning the Trie during the search by removing empty child nodes and marking words as found after they are located prevent redundant computations and improve performance.
Complexity Analysis
Time Complexity: O(m * n * 4^L)
Space Complexity: O(N)
Topics
This problem involves: Array, String, Backtracking, Trie, Matrix.
Companies
Asked at: Airbnb, Aurora, Block, Capital One, Cisco, DoorDash, Google, Instacart, Karat, PayPal, Roblox, Snowflake, Two Sigma, Wix, Zoho, eBay.