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: false
💡 Note: The word "ac" cannot be placed anywhere on the board. No horizontal slots of length 2 exist due to '#' separators. Column 0 has 3 empty spaces but lacks proper boundaries (no '#' above or below the board). Column 2 contains ['a','c','a'] but "ac" doesn't match any 2-character substring.
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
Check if Word Can Be Placed In Crossword INPUT 3x3 Crossword Board # (empty) # (empty) (empty) # # c (empty) # = Blocked ' ' = Empty Letter = Filled word = "abc" ALGORITHM STEPS 1 Find Valid Slots Scan rows/cols for slots of length = word.length 2 Check Boundaries Slot must be bounded by # or board edge 3 Match Letters Each cell: empty OR matches word letter 4 Try Both Directions Left-Right, Right-Left Top-Bottom, Bottom-Top Found Slot (vertical): (0,1) (1,1) (2,1) empty empty 'c' FINAL RESULT Place "abc" top to bottom: # a # b # # c Direction Output: true 'c' at (2,1) matches word[2] Key Insight: The optimized slot-finding approach scans each row and column once to find valid placement slots. A slot is valid if: (1) bounded by # or edges, (2) length equals word length, (3) existing letters match. Try both forward and reverse directions. Time: O(m*n*k) where k = word length. Space: O(1). TutorialsPoint - Check if Word Can Be Placed In Crossword | Optimized Slot Finding
Asked in
Google 12 Amazon 8 Microsoft 6 Apple 4
28.5K Views
Medium Frequency
~25 min Avg. Time
842 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