Practice/Google/2018. Check if Word Can Be Placed In Crossword
CodingOptional
You are given a crossword puzzle grid represented as a 2D array of characters and a word. The grid contains:
' ' (space) representing empty cells'#' representing blocked cells (walls)Determine whether the given word can be placed in the crossword grid either horizontally (left-to-right or right-to-left) or vertically (top-to-bottom or bottom-to-top).
A word can be placed in a slot if:
'#') or grid boundaries on both ends' ') or contains the same letter as the corresponding position in the wordtrue if the word can be placed in at least one valid slot, false otherwise' ', '#', or lowercase English lettersExample 1:
` Input: grid = [ ['#', ' ', ' ', '#'], ['#', ' ', '#', '#'], ['#', ' ', ' ', ' '] ] word = "cat"
Output: true
Explanation: The word "cat" can be placed horizontally in the third row starting at column index 1. The slot has length 3, bounded by '#' on the left and the grid boundary on the right. `
Example 2:
` Input: grid = [ ['#', ' ', '#'], ['#', ' ', '#'], ['#', ' ', '#'] ] word = "dog"
Output: true
Explanation: The word "dog" can be placed vertically in column index 1. The slot spans all three rows and is bounded by '#' on both sides. `
Example 3:
` Input: grid = [ [' ', ' ', ' ', ' '], ['#', 'x', 'y', 'z'] ] word = "code"
Output: false
Explanation: No valid slot of length 4 exists where "code" matches the pattern. The second row has pre-filled letters that don't match "code". `