Check if Word Can Be Placed In Crossword - Problem
Imagine you're a crossword puzzle enthusiast working on a partially completed puzzle. You have a word that you want to place, but you need to check if it fits according to crossword rules!
You're given an m x n crossword board where:
- Lowercase letters represent already placed letters from solved words
- Spaces (' ') represent empty cells where new letters can be placed
- Hash symbols ('#') represent blocked cells (like the black squares in real crosswords)
A word can be placed horizontally (left-to-right or right-to-left) or vertically (top-to-bottom or bottom-to-top) if:
- ๐ซ It doesn't occupy any blocked cells ('#')
- โ Each letter goes in an empty cell (' ') or matches an existing letter
- ๐ No empty cells or letters are directly adjacent to the word's boundaries
Goal: Return true if the word can be placed anywhere on the board following these rules, false otherwise.
Input & Output
example_1.py โ Valid Horizontal Placement
$
Input:
board = [["#", " ", "#"], [" ", " ", "#"], ["#", "c", " "]], word = "abc"
โบ
Output:
true
๐ก Note:
The word 'abc' can be placed vertically starting at position (0,1) going downward. The 'a' goes in empty space (0,1), 'b' goes in empty space (1,1), and 'c' matches the existing 'c' at (2,1). The placement is properly isolated with blocked cells adjacent to the boundaries.
example_2.py โ No Valid Placement
$
Input:
board = [[" ", "#", "a"], ["#", " ", "#"], ["#", " ", "#"]], word = "abc"
โบ
Output:
false
๐ก Note:
No valid placement exists for 'abc'. Horizontal placements are blocked by insufficient space or conflicting characters. Vertical placements fail due to isolation requirements - existing 'a' at (0,2) would be adjacent to any vertical word placement.
example_3.py โ Reverse Direction Placement
$
Input:
board = [["#", " ", " "], ["#", " ", " "], ["#", " ", "c"]], word = "ca"
โบ
Output:
true
๐ก Note:
The word 'ca' can be placed vertically in reverse direction (bottom-to-top) starting at (2,2). The 'c' matches existing 'c' at (2,2) and 'a' goes in empty space (1,2). This placement satisfies isolation since column is bounded by blocked cells.
Visualization
Tap to expand
Understanding the Visualization
1
Survey the Scene
Examine the crossword board layout, noting blocked cells (#), empty spaces ( ), and existing letters
2
Try Every Starting Point
For each cell position, consider it as a potential starting point for word placement
3
Check All Directions
Attempt placement in horizontal (left-to-right and right-to-left) and vertical (top-to-bottom and bottom-to-top) directions
4
Validate Placement Rules
Ensure word fits in bounds, doesn't hit blocked cells, matches existing letters, and maintains proper isolation
5
Report Findings
Return true if any valid placement is found, false if all positions fail validation
Key Takeaway
๐ฏ Key Insight: The systematic enumeration approach is optimal because we must check placement constraints for every possible position and direction. There's no way to eliminate positions without actually validating the crossword rules.
Time & Space Complexity
Time Complexity
O(m ร n ร w)
For each of the mรn cells, we potentially check word placement in 4 directions, each taking O(w) time where w is word length
โ Linear Growth
Space Complexity
O(1)
Only using constant extra space for variables and direction vectors
โ Linear Space
Constraints
- m == board.length
- n == board[i].length
- 1 โค m ร n โค 2 ร 105
- 1 โค word.length โค max(m, n)
- board[i][j] is either ' ', '#', or a lowercase English letter
- word consists of lowercase English letters only
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code