Check whether any 2x2 square in a 3x3 grid can be made monochrome by changing at most one cell.
Problem
You are given a grid of characters, where each cell is either B or W.
Determine whether it is possible to make at least one sub-square contain the same color in all four cells by changing the color of at most one cell in the entire grid.
A change means flipping a cell from B to W or from W to B.
Return true if such a square can be formed, otherwise return false.
Intuition
Because the grid is fixed at , you can inspect every possible block and count how many cells are already the same color. If any block has at least three matching cells, one flip is enough.
Input Format
- The input is a grid of characters.
- Each cell is one of
BorW.
Output Format
- Return a boolean value.
trueif some sub-square can be made monochrome with at most one flip, otherwisefalse.
Constraints
- The grid size is fixed at .
- Each cell contains exactly one of two colors:
BorW. - At most one cell may be changed.
Example 1
Input
grid = [["B","W","B"],["B","B","W"],["W","B","B"]]
Output
true
Explanation
The top-left 2x2 block contains B, W, B, B, so changing the single W to B makes it monochrome.
Example 2
Input
grid = [["B","W","B"],["W","B","W"],["B","W","B"]]
Output
false
Explanation
Every 2x2 block contains at least two B cells and two W cells, so one flip is not enough to make any block uniform.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.