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:

  1. ๐Ÿšซ It doesn't occupy any blocked cells ('#')
  2. โœ… Each letter goes in an empty cell (' ') or matches an existing letter
  3. ๐Ÿ”’ 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
๐Ÿ•ต๏ธโ€โ™€๏ธ Crossword Detective InvestigationEvidence Board:#็ฉบ#็ฉบ็ฉบ##c็ฉบInvestigation: Word "abc"Vertical placement found!abcmatches!Detective's Checklist:โœ“Word fits in boundsโœ“No blocked cells hitโœ“Letters compatibleโœ“Proper isolationCASE CLOSED: TRUEInvestigation ProcessTry every cell โ†’ Check all 4 directions โ†’ Validate placement rules โ†’ Report findingsTime Complexity: O(m ร— n ร— word_length) | Space: O(1)
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables and direction vectors

n
2n
โœ“ 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
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
29.0K Views
Medium Frequency
~25 min Avg. Time
854 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen