FastPrepValidate a Nonogram

Validate a Nonogram

WhatNot logoWhatNot● MediumINTERNPHONE SCREEN
Learn

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 1 cells. Separate runs have at least one 0 between them.
  • Run lengths must match in order, not just in total.
  • An empty clue list means that the line contains no 1 cells.
  • Validate the supplied matrix; do not solve the puzzle or change its cells.

Function

validateNonogram(grid: int[][], rowClues: int[][], colClues: int[][]) → boolean

Examples

Example 1

grid = [[1,1,0],[0,1,0],[1,1,1]]rowClues = [[2],[1],[3]]colClues = [[1,1],[3],[1]]return = true

The 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 = false

The 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 = true

Every row and column is empty of filled cells, so each clue is an empty list.

Constraints

  • 1 <= grid.length <= 20 and all rows have the same length c, with 1 <= c <= 20.
  • Each grid value is 0 or 1.
  • rowClues.length == grid.length and colClues.length == c.
  • A row clue list contains at most c values, each from 1 to c.
  • A column clue list contains at most grid.length values, each from 1 to grid.length.
  • Clues may be inconsistent with the grid or impossible to fit; return false in either case.

More WhatNot problems

See WhatNot hiring insights
public boolean validateNonogram(int[][] grid, int[][] rowClues, int[][] colClues) {
    // write your code here
}
grid[[1,1,0],[0,1,0],[1,1,1]]
rowClues[[2],[1],[3]]
colClues[[1,1],[3],[1]]
expectedtrue
Checking account…