Search Words in a Character Grid
Search Words in a Character Grid
You are presented with a 2D grid composed of lowercase English letters.
Part 1: Single Word Search
Given a single target word, determine if this word can be formed by traversing a path within the grid. A valid path must adhere to the following rules:
- It can start from any cell in the grid.
- Each subsequent character in the path must be located in a cell horizontally or vertically adjacent to the current cell.
- A grid cell cannot be used more than once within the same path.
Part 2: Multiple Word Search (Follow-up)
As a follow-up, you will be provided with a list of target words. Your task is to return all words from this list that can be formed in the grid, following the same adjacency and cell reuse rules as in Part 1. The solution for this part must be designed for efficiency, especially considering scenarios where the list of target words is large and many words within the list share common prefixes.
Implementation Requirements
For both parts, implement the necessary function(s) and provide a detailed analysis of their expected time and space complexity. Ensure your solution robustly handles various edge cases, including:
- Empty grids.
- Empty target word lists.
- Duplicate words within the target list.
- Target words that are longer than the total number of cells available in the grid.
AI interview prep
Core dilemma
Efficiently searching a graph (the grid) for multiple string patterns (words) while managing state (visited cells) and leveraging commonalities (shared prefixes) to avoid redundant computations.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Search Words in a Character Grid
Search Words in a Character Grid
You are presented with a 2D grid composed of lowercase English letters.
Part 1: Single Word Search
Given a single target word, determine if this word can be formed by traversing a path within the grid. A valid path must adhere to the following rules:
- It can start from any cell in the grid.
- Each subsequent character in the path must be located in a cell horizontally or vertically adjacent to the current cell.
- A grid cell cannot be used more than once within the same path.
Part 2: Multiple Word Search (Follow-up)
As a follow-up, you will be provided with a list of target words. Your task is to return all words from this list that can be formed in the grid, following the same adjacency and cell reuse rules as in Part 1. The solution for this part must be designed for efficiency, especially considering scenarios where the list of target words is large and many words within the list share common prefixes.
Implementation Requirements
For both parts, implement the necessary function(s) and provide a detailed analysis of their expected time and space complexity. Ensure your solution robustly handles various edge cases, including:
- Empty grids.
- Empty target word lists.
- Duplicate words within the target list.
- Target words that are longer than the total number of cells available in the grid.