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.
Since the grid is only 3x3, we can try changing each cell (or none) to either ‘B’ or ‘W’ and check if any 2x2 square becomes monochromatic. This brute force is feasible due to the small size.
classSolution {
public:bool canMakeSquare(vector<vector<char>>& grid) {
for (int i =0; i <3; ++i) {
for (int j =0; j <3; ++j) {
for (char c : {'B', 'W'}) {
auto g = grid;
g[i][j] = c;
if (check(g)) return true;
}
}
}
returncheck(grid);
}
boolcheck(vector<vector<char>>& g) {
for (int i =0; i <2; ++i) {
for (int j =0; j <2; ++j) {
char c = g[i][j];
if (g[i][j+1] == c && g[i+1][j] == c && g[i+1][j+1] == c) return true;
}
}
return false;
}
};
classSolution {
publicbooleancanMakeSquare(char[][] grid) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
for (char c : newchar[]{'B', 'W'}) {
char[][] g =newchar[3][3];
for (int x = 0; x < 3; ++x) g[x]= grid[x].clone();
g[i][j]= c;
if (check(g)) returntrue;
}
}
}
return check(grid);
}
booleancheck(char[][] g) {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
char c = g[i][j];
if (g[i][j+1]== c && g[i+1][j]== c && g[i+1][j+1]== c) returntrue;
}
}
returnfalse;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funcanMakeSquare(grid: Array<CharArray>): Boolean {
funcheck(g: Array<CharArray>): Boolean {
for (i in0..1) for (j in0..1) {
val c = g[i][j]
if (g[i][j+1] == c && g[i+1][j] == c && g[i+1][j+1] == c) returntrue }
returnfalse }
for (i in0..2) for (j in0..2) for (c in charArrayOf('B','W')) {
val g = Array(3) { grid[it].clone() }
g[i][j] = c
if (check(g)) returntrue }
return check(grid)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defcanMakeSquare(self, grid: list[list[str]]) -> bool:
defcheck(g):
for i in range(2):
for j in range(2):
c = g[i][j]
if g[i][j+1] == c and g[i+1][j] == c and g[i+1][j+1] == c:
returnTruereturnFalsefor i in range(3):
for j in range(3):
for c in'BW':
g = [row[:] for row in grid]
g[i][j] = c
if check(g):
returnTruereturn check(grid)
impl Solution {
pubfncan_make_square(grid: Vec<Vec<char>>) -> bool {
fncheck(g: &Vec<Vec<char>>) -> bool {
for i in0..2 {
for j in0..2 {
let c = g[i][j];
if g[i][j+1] == c && g[i+1][j] == c && g[i+1][j+1] == c {
returntrue;
}
}
}
false }
for i in0..3 {
for j in0..3 {
for&c in ['B','W'].iter() {
letmut g = grid.clone();
g[i][j] = c;
if check(&g) {
returntrue;
}
}
}
}
check(&grid)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
canMakeSquare(grid: string[][]):boolean {
functioncheck(g: string[][]):boolean {
for (leti=0; i<2; ++i) for (letj=0; j<2; ++j) {
constc=g[i][j];
if (g[i][j+1] ===c&&g[i+1][j] ===c&&g[i+1][j+1] ===c) returntrue;
}
returnfalse;
}
for (leti=0; i<3; ++i) for (letj=0; j<3; ++j) for (constcof ['B','W']) {
constg=grid.map(row=>row.slice());
g[i][j] =c;
if (check(g)) returntrue;
}
returncheck(grid);
}
}