You are given an grid representing a crossword board and a target string word. The grid contains empty spaces (' '), walls ('#'), and pre-filled letters. You need to determine if the word can be placed in any horizontal or vertical slot that is bounded by walls or the grid edges. The word must fit exactly—it cannot be shorter than the slot.
Google uses this "Medium" problem to test your Matrix traversal and Enumeration skills. It’s a test of whether you can handle multidimensional constraints and multiple directions (Forward, Backward, Horizontal, Vertical). It evaluations your attention to detail regarding slot boundaries and matching rules.
The pattern is Enumeration and Linear Scan. You iterate through every possible starting position and direction. However, a more efficient way is to find all "slots" (sequences of non-wall cells). For each slot, you check if the target word (or its reverse) matches the characters in that slot and if the slot length is exactly equal to the word length.
Grid: ' ' ' ' '#' 'a' ' ' '#' Target word: "ca"
A common error is forgetting to check the word in reverse order (e.g., "ca" can be placed as "ac" in a slot). Another is allowing a word to be placed in a slot that is longer than the word itself. Candidates also often struggle with the logic for splitting rows and columns into slots efficiently.
For matrix problems with many conditions, try to isolate the "candidate" regions first. In this case, finding all the "slots" (using a wall as a delimiter) simplifies the problem into a simple string matching task.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Check if Move is Legal | Medium | Solve | |
| Find the Minimum Area to Cover All Ones II | Hard | Solve | |
| Image Overlap | Medium | Solve | |
| Find Champion I | Easy | Solve | |
| Collecting Chocolates | Medium | Solve |