Problem Statement
A chessboard consists of alternating black and white squares: every square differs in color from the squares that are adjacent to it vertically and horizontally.
You are given a rectangular board of black and white squares in a String[] board. However, the coloring of the squares might not match the chessboard coloring scheme. Each string in board represents a row of squares of the board, and each character (all either 'B' or 'W') represents a square in that row. Return the minimum number of squares that would need their color swapped for the given board to become a valid chessboard.
Definition
- Class:
- BrokenChessboard
- Method:
- minimumFixes
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int minimumFixes(String[] board)
- (be sure your method is public)
Constraints
- board will contain between 1 and 50 elements, inclusive.
- Each element of board will contain between 1 and 50 characters, inclusive.
- Each element of board will be the same length.
- Each character of each element of board will be either 'B' or 'W'.
Examples
{"BWB", "BBW", "BWW"}Returns: 2
There are two ways to fix this board: BWB WBW WBW BWB BWB WBW The result on the left would require changing 2 squares (center-left and bottom-right). The result on the right would mean changing 7 squares, so the former is a better choice.{"WW", "WW"}Returns: 2
Either of the two valid boards we could make would take exactly 2 changes.{"BWB", "WBW", "BWB"}Returns: 0
This board is already valid, so no fixes are needed.{"B", "W", "B", "B"}Returns: 1
The board is not square, but anyways it only needs 1 square changed.{"BWBBB", "WWBBW", "BBBBW", "WBWBB"}Returns: 7