Problem · Matrix
Validate a Two-Color Chessboard
Learn this problemProblem statement
You are given a nonempty rectangular integer matrix board. Every cell is one of two colors, encoded as 0 or 1.
Return true if board is a valid chessboard pattern: every pair of horizontally adjacent cells has different colors, and every pair of vertically adjacent cells has different colors. Otherwise, return false.
Function
isValidChessboard(board: int[][]) → booleanExamples
Example 1
board = [[0,1,0],[1,0,1]]return = trueEvery horizontal and vertical neighbor has the opposite color.
Example 2
board = [[0,1],[1,1]]return = falseThe two cells in the bottom row have the same color, so the pattern is invalid.
Constraints
1 <= board.length <= 1001 <= board[i].length <= 100- Every row has the same length.
board[i][j]is either0or1.