Leetcode #2018: Check if Word Can Be Placed In Crossword
In this guide, we solve Leetcode #2018 Check if Word Can Be Placed In Crossword in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
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.
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: Array, Enumeration, Matrix
Intuition
Grid problems are easiest when you define clear row/column boundaries.
A consistent traversal order prevents off-by-one errors.
Approach
Iterate by rows, columns, or layers depending on the requirement.
Keep bounds updated as the traversal progresses.
Steps:
- Define bounds or directions.
- Visit cells in order.
- Update result and move bounds.
Example
Input: board = [["#", " ", "#"], [" ", " ", "#"], ["#", "c", " "]], word = "abc"
Output: true
Explanation: The word "abc" can be placed as shown above (top to bottom).
Python Solution
class Solution:
def placeWordInCrossword(self, board: List[List[str]], word: str) -> bool:
def check(i, j, a, b):
x, y = i + a * k, j + b * k
if 0 <= x < m and 0 <= y < n and board[x][y] != '#':
return False
for c in word:
if (
i < 0
or i >= m
or j < 0
or j >= n
or (board[i][j] != ' ' and board[i][j] != c)
):
return False
i, j = i + a, j + b
return True
m, n = len(board), len(board[0])
k = len(word)
for i in range(m):
for j in range(n):
left_to_right = (j == 0 or board[i][j - 1] == '#') and check(i, j, 0, 1)
right_to_left = (j == n - 1 or board[i][j + 1] == '#') and check(
i, j, 0, -1
)
up_to_down = (i == 0 or board[i - 1][j] == '#') and check(i, j, 1, 0)
down_to_up = (i == m - 1 or board[i + 1][j] == '#') and check(
i, j, -1, 0
)
if left_to_right or right_to_left or up_to_down or down_to_up:
return True
return False
Complexity
The time complexity is , and the space complexity is . The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.