Crossword Filler - Problem
You are given a crossword puzzle grid and a list of words to fill it with. The grid contains empty cells (represented by 0) and blocked cells (represented by 1). Your task is to place all the given words in the grid such that:
Words can be placed horizontally (left to right) or vertically (top to bottom)
Words must fit entirely within consecutive empty cells
All words must be used exactly once
Return the filled grid where words are represented by their characters, blocked cells remain as 1, and any remaining empty cells are 0. If no valid arrangement exists, return an empty array.
Input & Output
Example 1 — Basic 3x4 Grid
$
Input:
grid = [[0,1,0,0],[0,0,0,0],[0,0,1,0]], words = ["CAT","GO"]
›
Output:
[["C",1,"G","O"],["A",0,0,0],["T",0,1,0]]
💡 Note:
CAT placed vertically at (0,0) and GO placed horizontally at (0,2). Both words fit perfectly in the available slots.
Example 2 — Intersecting Words
$
Input:
grid = [[0,0,0],[0,0,0],[0,0,0]], words = ["AB","AC"]
›
Output:
[["A","B",0],["C",0,0],[0,0,0]]
💡 Note:
AB placed horizontally at (0,0) and AC placed vertically at (0,0). They share the letter A at position (0,0).
Example 3 — No Solution
$
Input:
grid = [[0,1,0],[1,1,1],[0,1,0]], words = ["HELLO"]
›
Output:
[]
💡 Note:
No continuous slot is long enough to fit the 5-letter word HELLO. Return empty array.
Constraints
- 1 ≤ grid.length, grid[0].length ≤ 20
- 1 ≤ words.length ≤ 10
- 1 ≤ words[i].length ≤ 20
- grid[i][j] is 0 or 1
- words[i] contains only uppercase letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code