3127. Make a Square with the Same Color LeetCode Solution

In this guide, you will get 3127. Make a Square with the Same Color LeetCode Solution with the best time and space complexity. The solution to Make a Square with the Same Color problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Make a Square with the Same Color solution in C++
  4. Make a Square with the Same Color solution in Java
  5. Make a Square with the Same Color solution in Python
  6. Additional Resources
3127. Make a Square with the Same Color LeetCode Solution image

Problem Statement of Make a Square with the Same Color

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:

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:

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:

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’.

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(1)

3127. Make a Square with the Same Color LeetCode Solution in C++

class Solution {
 public:
  bool canMakeSquare(vector<vector<char>>& grid) {
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j) {
        int black = 0;
        int white = 0;
        for (int x = 0; x < 2; ++x)
          for (int y = 0; y < 2; ++y)
            if (grid[i + x][j + y] == 'B')
              ++black;
            else
              ++white;
        if (black >= 3 || white >= 3)
          return true;
      }
    return false;
  }
};
/* code provided by PROGIEZ */

3127. Make a Square with the Same Color LeetCode Solution in Java

class Solution {
  public boolean canMakeSquare(char[][] grid) {
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j) {
        int black = 0;
        int white = 0;
        for (int x = 0; x < 2; ++x)
          for (int y = 0; y < 2; ++y)
            if (grid[i + x][j + y] == 'B')
              ++black;
            else
              ++white;
        if (black >= 3 || white >= 3)
          return true;
      }
    return false;
  }
}
// code provided by PROGIEZ

3127. Make a Square with the Same Color LeetCode Solution in Python

class Solution:
  def canMakeSquare(self, grid: list[list[str]]) -> bool:
    for i in range(2):
      for j in range(2):
        black = 0
        white = 0
        for x in range(2):
          for y in range(2):
            if grid[i + x][j + y] == 'B':
              black += 1
            else:
              white += 1
        if black >= 3 or white >= 3:
          return True
    return False
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.