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:
'#'.' ' (empty) or match the letter already on the board.' ' or other lowercase letters directly left or right of the word if the word was placed horizontally.' ' 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 * 105board[i][j] will be ' ', '#', or a lowercase English letter.1 <= word.length <= max(m, n)word will contain only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem is all about trying every possible placement of the word in the crossword. We will explore every location and orientation to see if the word fits and matches existing letters.
Here's how the algorithm would work step-by-step:
def can_place_word_in_crossword(board, word):
rows = len(board)
cols = len(board[0])
word_length = len(word)
# Check horizontal placement
for row_index in range(rows):
for col_index in range(cols - word_length + 1):
valid = True
# Check if word fits and matches
for word_index in range(word_length):
if board[row_index][col_index + word_index] != ' ' and \
board[row_index][col_index + word_index] != word[word_index]:
valid = False
break
if valid:
return True
# Check vertical placement
for row_index in range(rows - word_length + 1):
for col_index in range(cols):
valid = True
# Check if word fits and matches
for word_index in range(word_length):
if board[row_index + word_index][col_index] != ' ' and \
board[row_index + word_index][col_index] != word[word_index]:
valid = False
break
if valid:
return True
# No valid placement found
return FalseThe task is to check if a given word can be placed into a crossword puzzle. The optimal strategy involves checking possible locations and directions for the word, ensuring it fits within the crossword's boundaries and matches existing letters where applicable. We'll focus on quickly eliminating invalid placements to find a valid one efficiently.
Here's how the algorithm would work step-by-step:
def can_place_word_in_crossword(crossword, word):
rows = len(crossword)
cols = len(crossword[0])
word_length = len(word)
for start_row in range(rows):
for start_col in range(cols):
# Check horizontal placement
if can_place_horizontally(crossword, word, start_row, start_col):
return True
# Check vertical placement
if can_place_vertically(crossword, word, start_row, start_col):
return True
return False
def can_place_horizontally(crossword, word, start_row, start_col):
cols = len(crossword[0])
word_length = len(word)
# Check if the word fits within bounds
if start_col + word_length > cols:
return False
for i in range(word_length):
# Check if letters match or space is empty
cell = crossword[start_row][start_col + i]
if cell != ' ' and cell != word[i]:
return False
return True
def can_place_vertically(crossword, word, start_row, start_col):
rows = len(crossword)
word_length = len(word)
# Check if the word fits within bounds
if start_row + word_length > rows:
return False
for i in range(word_length):
# Check if letters match or space is empty
cell = crossword[start_row + i][start_col]
if cell != ' ' and cell != word[i]:
return False
return True| Case | How to Handle |
|---|---|
| Null or empty crossword or word input | Return false immediately as a valid placement is impossible. |
| Word length is greater than any dimension of the crossword | Return false, as the word cannot fit in either direction. |
| Crossword contains invalid characters (not ' ' or letter) | Throw an IllegalArgumentException or return false after validation. |
| Word contains invalid characters (not letters) | Throw an IllegalArgumentException or return false after validation. |
| Crossword is a single row or column | The algorithm should still correctly check if the word can be placed horizontally or vertically, respectively. |
| Multiple possible placements exist | The problem only asks to check if *a* valid placement exists, so return true as soon as one is found. |
| Word contains consecutive repeating characters | The algorithm should correctly handle consecutive identical letters in both the word and the crossword. |
| Crossword is very large, causing potential performance issues | Optimize the search by immediately returning when a valid placement is found, avoiding unnecessary iterations. |