Skip to main content
Back to problems
Leetcode
Medium
Arrays
Matrices
Simulation
Make A Square With the Same Color

Check whether any 2x2 square in a 3x3 grid can be made monochrome by changing at most one cell.

Acceptance 0%
Problem Statement

Problem

You are given a 3×33 \times 3 grid of characters, where each cell is either B or W.

Determine whether it is possible to make at least one 2×22 \times 2 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 3×33 \times 3, you can inspect every possible 2×22 \times 2 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 3×33 \times 3 grid of characters.
  • Each cell is one of B or W.

Output Format

  • Return a boolean value.
  • true if some 2×22 \times 2 sub-square can be made monochrome with at most one flip, otherwise false.

Constraints

  • The grid size is fixed at 3×33 \times 3.
  • Each cell contains exactly one of two colors: B or W.
  • At most one cell may be changed.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.