Problem Description
Given an m x n board representing the state of a crossword puzzle, determine if a given word can be placed on the board. Cells in the board may contain: • a lowercase English letter (from a solved word) • ' ' (empty cell) • '#' (blocked cell) A word can be placed horizontally (left to right or right to left) or vertically (top to bottom or bottom to top) if: • No letter is placed in a '#' cell. • Each cell used is either empty or already contains the same letter as in the word. • If placed horizontally, the cells immediately to the left and right of the word (if within board bounds) must be blocked ('#') or off the board. • If placed vertically, the cells immediately above and below the word (if within board bounds) must be blocked ('#') or off the board. The task is to return true if the word can be placed in any possible valid position according to the rules above; otherwise, return false.
Key Insights
- Iterate over the board horizontally and vertically to identify continuous segments (slots) of cells that are not blocked.
- For each identified candidate slot, check: • The length of the slot should match the word’s length. • Each cell in the slot must either be empty (' ') or already contain the matching letter. • The slot must be isolated: cells adjacent to the slot (left/right for horizontal, above/below for vertical) should not be part of an ongoing word (i.e. must be blocked or outside the board) to ensure a valid word placement.
- Check both the original word and its reverse to account for placement in either direction.
Space and Time Complexity
Time Complexity: O(m * n * L) where m and n are board dimensions and L is the length of the word. We scan each cell and validate segments of length up to L. Space Complexity: O(1) extra space (not counting the input board and word).
Solution
We solve the problem by enumerating every potential slot in the board:
- For horizontal checks:
- For each row, iterate over the columns and split the row into segments separated by '#'.
- For each segment, verify if its length is equal to the word’s length.
- Ensure that the segment is isolated from adjacent cells on its left and right (if any exist).
- Check if for every position the board cell is either empty or equals the corresponding letter of the word. Also, check the reversed word.
- For vertical checks:
- Similar to horizontal, iterate over each column and collect segments from consecutive rows, using '#' as delimiters.
- Verify length, check adjacent boundaries (above and below), and validate the match for both orientations.
- If any segment satisfies the conditions, return true. Otherwise, return false after all possibilities have been exhausted. A key detail is verifying the boundaries before and after each segment to ensure that they are either blocked or off the board.