Problem Statement
You are given the String[] board that represents a standard 8x8 chessboard. In particular, the character '.' represents an empty square, the character 'W' is a square with a white rook, and the character 'B' is a square with a black rook.
Two rooks attack each other if:
- They have opposite colors.
- They share the same row or the same column.
- Between them, all the squares in that row/column are empty.
Return the number of pairs of rooks that attack each other.
Definition
- Class:
- WarmupWithRooks
- Method:
- count
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int count(String[] board)
- (be sure your method is public)
Constraints
- board will contain exactly 8 elements.
- Each element of board will contain exactly 8 characters.
- Each character in board will be one of '.', 'W', and 'B'.
Examples
{"........", "........", "........", "........", "........", "........", "........", "........"}Returns: 0
This is an empty board. There are no pairs of attacking rooks.{"........", "...W.W..", "........", "........", "........", "........", "B.......", ".B......"}Returns: 0
Two of these four rooks share the same row but as they are both white they do not count as attacking each other.{"W......B", "........", "........", "........", "........", "........", "........", "B......W"}Returns: 4
{"........", "........", "........", "WWWWBBBB", "........", "........", "........", "........"}Returns: 1
Only the rightmost white rook and the leftmost black rook attack each other.{"........", ".WB..W.W", "........", ".......B", "..W..BB.", "..B...W.", "..BW...B", ".B..B..W"}Returns: 14