Leetcode 2018: Check if Word Can Be Placed In Crossword

grid47
grid47
Exploring patterns and algorithms
Apr 19, 2024 8 min read

You are given an m x n matrix representing a crossword puzzle. The matrix contains lowercase English letters, empty spaces (’ ‘), and blocked cells (’#’). Given a word, determine if it can be placed horizontally or vertically in the crossword while adhering to the following constraints.
Problem
Approach
Steps
Complexity
Input: The input consists of a matrix board of size m x n, where each element is either a lowercase letter, an empty space (' '), or a blocked cell ('#'). A string word is also provided, representing the word that needs to be placed on the board.
Example: [['#', ' ', '#'], [' ', ' ', '#'], ['#', 'c', ' ']]
Constraints:
• m == board.length
• n == board[i].length
• 1 <= m * n <= 2 * 10^5
• board[i][j] will be ' ', '#', or a lowercase English letter.
• 1 <= word.length <= max(m, n)
Output: Return true if the word can be placed on the board, otherwise return false.
Example: true
Constraints:
• The output will be a boolean value indicating whether the word can be placed on the board.
Goal: The goal is to check if the word can be placed horizontally or vertically on the crossword puzzle, considering all given constraints.
Steps:
• For each row and column, check if the word fits in either direction (horizontal or vertical) with the given constraints.
• Ensure that the word does not overlap any blocked cells.
• Verify that there are no conflicting adjacent cells before and after placing the word.
Goal: The matrix board contains only lowercase letters, spaces, and blocked cells. The word must fit within the constraints of the board dimensions.
Steps:
• board[i][j] will be either a lowercase letter, ' ', or '#'.
• 1 <= m * n <= 2 * 10^5
• 1 <= word.length <= max(m, n)
Assumptions:
• The word can be placed horizontally or vertically.
• The grid is a crossword puzzle containing blocked cells, empty spaces, and already placed words.
Input: [['#', ' ', '#'], [' ', ' ', '#'], ['#', 'c', ' ']]
Explanation: The word 'abc' can be placed vertically starting at (0, 1) without conflicting with other letters or blocked cells.

Input: [[' ', '#', 'a'], [' ', '#', 'c'], [' ', '#', 'a']]
Explanation: It is impossible to place the word 'ac' because of the blocked cells and no available continuous empty space.

Input: [['#', ' ', '#'], [' ', ' ', '#'], ['#', ' ', 'c']]
Explanation: The word 'ca' can be placed horizontally from right to left in the third row.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus