Make a Square with the Same Color - Problem
You're given a 3×3 grid filled with black ('B') and white ('W') cells, like a mini chessboard pattern. Your challenge is to determine if you can create a 2×2 square where all four cells have the same color by changing at most one cell.

Think of it as a puzzle game where you need to find if there's already a monochrome 2×2 square, or if you can create one with just a single color flip. There are exactly four possible 2×2 squares in a 3×3 grid:
  • Top-left: positions (0,0), (0,1), (1,0), (1,1)
  • Top-right: positions (0,1), (0,2), (1,1), (1,2)
  • Bottom-left: positions (1,0), (1,1), (2,0), (2,1)
  • Bottom-right: positions (1,1), (1,2), (2,1), (2,2)
Return true if it's possible to achieve this goal, false otherwise.

Input & Output

example_1.py — Basic Example
$ Input: [["B","W","B"],["B","W","W"],["B","W","B"]]
Output: true
💡 Note: We can change the cell at (0,1) from 'W' to 'B' to create a 2×2 black square in the top-left corner: positions (0,0), (0,1), (1,0), (1,1) would all be 'B'.
example_2.py — Already Valid
$ Input: [["B","W","B"],["W","W","W"],["B","W","B"]]
Output: true
💡 Note: The top-right 2×2 square at positions (0,1), (0,2), (1,1), (1,2) contains ['W','B','W','W']. We can change the 'B' at (0,2) to 'W' to make all four cells white with just 1 change.
example_3.py — Impossible Case
$ Input: [["B","W","B"],["W","B","W"],["B","W","B"]]
Output: false
💡 Note: This is a perfect checkerboard pattern. Every 2×2 square contains exactly 2 black and 2 white cells, so each would need 2 changes to become monochrome. Since we can change at most 1 cell, it's impossible.

Visualization

Tap to expand
All Four Possible 2×2 Squares in 3×3 GridSquare 1Changes: min(2,2)=2Square 2Changes: min(2,2)=2Square 3Changes: min(2,2)=2Square 4Changes: min(2,2)=2Example Analysis: Perfect Checkerboard Pattern• Every 2×2 square has exactly 2 black and 2 white cells• Each square needs min(2,2) = 2 changes to become monochrome• Since we can change at most 1 cell, this pattern returns FALSE🔥 Key Insight: Look for squares that already have 3+ cells of the same color!
Understanding the Visualization
1
Identify Candidates
There are exactly 4 possible 2×2 squares in a 3×3 grid
2
Count Colors
For each 2×2 area, count black vs white tiles
3
Calculate Changes
Changes needed = min(blacks, whites) to make it monochrome
4
Find Solution
If any area needs ≤1 change, we have a solution!
Key Takeaway
🎯 Key Insight: In a 3×3 grid, systematically check all 4 possible 2×2 squares. If any square has 3 or 4 cells of the same color, you can make it monochrome with ≤1 change!

Time & Space Complexity

Time Complexity
⏱️
O(1)

We check exactly 4 squares with 4 cells each = 16 operations, which is constant

n
2n
Linear Growth
Space Complexity
O(1)

Only using a few variables to count colors, no additional data structures needed

n
2n
Linear Space

Constraints

  • The grid is always exactly 3 × 3
  • Each cell contains only 'B' (black) or 'W' (white)
  • You can change at most 1 cell
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen