36. Valid Sudoku
Leetcode: https://leetcode.com/problems/valid-sudoku/
1 | def isValidSudoku(self, board: List[List[str]]) -> bool: |
Valid grid
A nonogram is a logic puzzle, similar to a crossword, in which the player is given a blank grid and has to color it according to some instructions. Specifically, each cell can be either black or white, which we will represent as ‘B’ for black and ‘W’ for white.
+——————+
| W W W W |
| B W W W |
| B W B B |
| W W B W |
| B B W W |
+——————+
For each row and column, the instructions give the lengths of contiguous runs of black (‘B’) cells. For example, the instructions for one row of [ 2, 1 ] indicate that there must be a run of two black cells, followed later by another run of one black cell, and the rest of the row filled with white cells.
These are valid solutions: [ W, B, B, W, B ] and [ B, B, W, W, B ] and also [ B, B, W, B, W ]
This is not valid: [ W, B, W, B, B ] since the runs are not in the correct order.
This is not valid: [ W, B, B, B, W ] since the two runs of Bs are not separated by Ws.
Your job is to write a function to validate a possible solution against a set of instructions. Given a 2D matrix representing a player’s solution; and instructions for each row along with additional instructions for each column; return True or False according to whether both sets of instructions match.
Example instructions #1
matrix1 = [[ W, W, W, W ],
[ B, W, W, W ],
[ B, W, B, B ],
[ W, W, B, W ],
[ B, B, W, W ]]
rows1_1 = [], [1], [1,2], [1], [2]
columns1_1 = [2,1], [1], [2], [1]
validateNonogram(matrix1, rows1_1, columns1_1) => True
1 | def validateNonogram(matrix, rows, cols): |