A square chessboard is painted using two colours, represented by 0 and 1. The board is provided as a list of comma-separated row strings.
Determine whether the board is valid. A chessboard is valid when every pair of cells that share a side have different colours.
ChessboardValidator
ChessboardValidator()
public boolean isValidChessboard(List<String> board)
board.get(row) contains the comma-separated colours in the row at zero-based position row.true if every side-adjacent pair of cells have different colours.false otherwise.1 ≤ board.size() ≤ 1,000n = board.size().n values separated by commas, so the board has n rows and n columns.0 or 1.board and all its elements are non-null.O(n2) time.O(1) additional space.The method returns only a boolean value. It returns true exactly when the complete board satisfies the adjacency rule.
isValidChessboard( board = List.of( "1,0,1,0", "0,1,0,1", "1,0,1,0", "0,1,0,1"))
Output: true
Every horizontal and vertical neighbor has the opposite colour.
isValidChessboard( board = List.of( "0,1,0", "1,0,1", "0,1,1"))
Output: false
The cells at positions (2, 1) and (2, 2) share a side and both have colour 1.
isValidChessboard(board = List.of("0"))
Output: true
A one-cell board has no adjacent pair that can violate the rule.