Check if Word Can Be Placed In Crossword Problem
Description
LeetCode Problem 2018.
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 theboard. - 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.
Example 1:

Input: board = [["#", " ", "#"], [" ", " ", "#"], ["#", "c", " "]], word = "abc"
Output: true
Explanation: The word "abc" can be placed as shown above (top to bottom).
Example 2:

Input: board = [[" ", "#", "a"], [" ", "#", "c"], [" ", "#", "a"]], word = "ac"
Output: false
Explanation: It is impossible to place the word because there will always be a space/letter above or below it.
Example 3:

Input: board = [["#", " ", "#"], [" ", " ", "#"], ["#", " ", "c"]], word = "ca"
Output: true
Explanation: The word "ca" can be placed as shown above (right to left).
Constraints:
m == board.lengthn == board[i].length1 <= m * n <= 2 * 10^5board[i][j]will be' ','#', or a lowercase English letter.1 <= word.length <= max(m, n)wordwill contain only lowercase English letters.
Solution
Approach: Enumeration
Try every cell as the first letter of the word, in each of the four directions (left to right, right to left, top to bottom, bottom to top).
A cell can start a placement in direction (a, b) only if the cell just behind it is the border of the board or '#'. So for left to right we need j == 0 or board[i][j - 1] == '#', and similarly for the other three directions.
Then check(i, j, a, b) decides whether the word fits, with k the length of the word:
- The cell just past the end of the word,
(i + a * k, j + b * k), must be outside the board or'#'. If it is inside the board and not'#', the slot is longer than the word. - Walk along the direction. Every cell must be inside the board and be either a space or the matching letter of the word.
Complexity: O(mn * k) in the worst case, but each cell only starts a walk in a direction if it is the start of a slot, so the total work is O(mn). O(1) extra space.
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