3127. Make a Square with the Same Color

3127. Make a Square with the Same Color

Description

You are given a 2D matrix grid of size 3 x 3 consisting only of characters 'B' and 'W'. Character 'W' represents the white color, and character 'B' represents the black color.

Your task is to change the color of at most one cell so that the matrix has a 2 x 2 square where all cells are of the same color.

Return true if it is possible to create a 2 x 2 square of the same color, otherwise, return false.

Example 1:

1
2
3
4
5
6
7
Input: grid = [["B","W","B"],["B","W","W"],["B","W","B"]]

Output: true

Explanation:

It can be done by changing the color of the `grid[0][2]`.

Example 2:

1
2
3
4
5
6
7
Input: grid = [["B","W","B"],["W","B","W"],["B","W","B"]]

Output: false

Explanation:

It cannot be done by changing at most one cell.

Example 3:

1
2
3
4
5
6
7
Input: grid = [["B","W","B"],["B","W","W"],["B","W","W"]]

Output: true

Explanation:

The `grid` already contains a `2 x 2` square of the same color.

Constraints:

  • grid.length == 3
  • grid[i].length == 3
  • grid[i][j] is either 'W' or 'B'.

Hints/Notes

  • N/A

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
bool canMakeSquare(vector<vector<char>>& grid) {
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 1; j++) {
// left = i, up = j
vector<pair<int, int>> square = squareColor(i, j);
int whiteCount = 0;
for (auto cordinate : square) {
if (grid[cordinate.first][cordinate.second] == 'W') {
whiteCount++;
}
}
if (whiteCount != 2) {
return true;
}
}
}
return false;
}

vector<pair<int, int>> squareColor(int i, int j) {
vector<pair<int, int>> res;
res.push_back({i, j});
res.push_back({i + 1, j});
res.push_back({i, j + 1});
res.push_back({i + 1, j + 1});
return res;
}
};