Validate a Nonogram
Problem statement
A nonogram clue lists the lengths of the consecutive filled runs in one row or column. A filled cell is 1 and an empty cell is 0.
Given a rectangular binary matrix grid, one clue list per row in rowClues, and one clue list per column in colClues, return true exactly when every clue matches the matrix.
- Read each row from left to right and each column from top to bottom.
- Each clue value describes one maximal consecutive run of
1cells. Separate runs have at least one0between them. - Run lengths must match in order, not just in total.
- An empty clue list means that the line contains no
1cells. - Validate the supplied matrix; do not solve the puzzle or change its cells.
Function
validateNonogram(grid: int[][], rowClues: int[][], colClues: int[][]) → booleanExamples
Example 1
grid = [[1,1,0],[0,1,0],[1,1,1]]rowClues = [[2],[1],[3]]colClues = [[1,1],[3],[1]]return = trueThe row runs are [2], [1], and [3]. Reading columns top to bottom gives [1,1], [3], and [1].
Example 2
grid = [[1,1,0],[0,1,0],[1,1,1]]rowClues = [[2],[1],[3]]colClues = [[2],[3],[1]]return = falseThe first column contains two separate runs of length 1, so the clue [2] is incorrect even though both contain two filled cells.
Example 3
grid = [[0,0],[0,0]]rowClues = [[],[]]colClues = [[],[]]return = trueEvery row and column is empty of filled cells, so each clue is an empty list.
Constraints
1 <= grid.length <= 20and all rows have the same lengthc, with1 <= c <= 20.- Each grid value is
0or1. rowClues.length == grid.lengthandcolClues.length == c.- A row clue list contains at most
cvalues, each from1toc. - A column clue list contains at most
grid.lengthvalues, each from1togrid.length. - Clues may be inconsistent with the grid or impossible to fit; return
falsein either case.