Check if Word Can Be Placed In Crossword - Problem
You are given an m x n matrix board, representing the current state of a crossword puzzle. The crossword contains lowercase English letters (from solved words), ' ' to represent any empty cells, and '#' to represent any blocked cells.
A word can be placed horizontally (left to right or right to left) or vertically (top to bottom or bottom to top) in the board if:
- It does not occupy a cell containing the character
'#'. - The cell each letter is placed in must either be
' '(empty) or match the letter already on the board. - There must not be any empty cells
' 'or other lowercase letters directly left or right of the word if the word was placed horizontally. - There must not be any empty cells
' 'or other lowercase letters directly above or below the word if the word was placed vertically.
Given a string word, return true if word can be placed in board, or false otherwise.
Input & Output
Example 1 — Word Fits Vertically
$
Input:
board = [["#", " ", "#"], [" ", " ", "#"], ["#", "c", " "]], word = "abc"
›
Output:
true
💡 Note:
The word "abc" can be placed vertically in column 1: board[0][1]=' ' becomes 'a', board[1][1]=' ' becomes 'b', board[2][1]='c' stays 'c'. The placement is valid as it's bounded by '#' cells.
Example 2 — Word Fits Horizontally
$
Input:
board = [[" ", "#", "a"], [" ", "#", "c"], [" ", "#", "a"]], word = "ac"
›
Output:
true
💡 Note:
The word "ac" can be placed horizontally in row 1, columns 0-1: board[1][0]=' ' becomes 'a', board[1][1]=' ' becomes 'c'. It's properly bounded by '#' on the right.
Example 3 — No Valid Placement
$
Input:
board = [["#", "#", "#"], [" ", " ", " "], ["#", "#", "#"]], word = "abc"
›
Output:
false
💡 Note:
The word "abc" has length 3, but the only available slot is the middle row with length 3. However, there are no boundaries ('#') at the ends of this slot, so the word cannot be placed.
Constraints
- m == board.length
- n == board[i].length
- 1 ≤ m, n ≤ 20
- board[i][j] will be ' ', '#', or a lowercase English letter
- 1 ≤ word.length ≤ max(m, n)
Visualization
Tap to expand
Understanding the Visualization
1
Input Board
3x3 crossword with spaces, blocked cells (#), and existing letters
2
Find Valid Slots
Identify continuous sequences bounded by '#' that match word length
3
Check Compatibility
Verify each character can be placed (empty space or matching letter)
Key Takeaway
🎯 Key Insight: Words must fit in continuous slots of exact length, bounded by '#' or board edges, with compatible characters
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code